1
\$\begingroup\$

I attached a a gameobject labeled "Gun" to my player's back and when the player is facing left I want the Gun to continue to look like it's still attached to his back. Right now when I play the game and the player is facing left, the gun stays in the same position it was in regardless of what direction the player is facing. How do I make the gun look like it's still on the players back if facing left? (sorry if it sounds like idk what i'm doing... I'm sorta new to game dev.)

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

make the weapon child of player and set weapon order in layer = 1

then you can do Scale X or Rotate Y to achieving flip effect.

enter image description here

void Flip(){
    Vector2 scale = transform.localScale;
    scale.x *= -1;
    transform.localScale = scale;
}

/* another method
    void Flip(){
        Vector2 currentRotation = transform.eulerAngles;
        currentRotation.y += 180;
        transform.localEulerAngles = currentRotation;
    }
*/

If you want to know player is facing left or right you can do this:

 private bool facingRight;

 float h = Input.GetAxisRaw("Horizontal");
 if(h > 0 && facingRight){
 Flip();
 }else if(h < 0 && !facingRight){
 Flip();
 }

void Flip(){
    facingRight = !facingRight;
    Vector2 scale = transform.localScale;
    scale.x *= -1;
    transform.localScale = scale;
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .