0
votes

I'm using Unity to move my player sprite in a 2D platformer where it'll go right if right key is pressed & left if left key is pressed.

The right movement is working fine, but whenever the player moves left, I'm changing the Y rotation to 180 and the player is supposed to show left animation only but it keeps rotating back and forth when going left.

See the video sample.

This is my code:

private Rigidbody2D rb;

private void FixedUpdate() {
    InputManager();
}

private void InputManager()
{
    float hDir = Input.GetAxis("Horizontal");

    if (state != State.hurt) {
        if(!anm.GetCurrentAnimatorStateInfo(0).IsTag("attack"))
        {
            if (hDir < 0)  // Go left
            {
                rb.velocity = new Vector2(-speedX, rb.velocity.y);
                transform.Rotate(0f, 180f, 0f);
            }
            else if (hDir > 0)  // Go right
            {
                rb.velocity = new Vector2(speedX, rb.velocity.y);
                transform.Rotate(0f, 0f, 0f);
            }
        }
    }
}

How can I make my player stick to left animation when going on left? Please do not suggest changing localScale as I know it works but for shooting purpose it's best that my player rotates.

2
Pretty sure rotate rotates the current transform by the given quarternion - so you are just spinning the player over and over. You probably want to set transform.rotation directly instead. - Charleh
Ah apparently that's not true.. interesting though since your code looks ok, do you have a gif/video clip of the issue? The one in the link doesn't seem to work - Charleh

2 Answers

2
votes

Here is a thing transform.Rotate(0,180,0); is used to rotate 180 degree in y axis each time when this line of code execute that's why you player does not stop rotating

Here is the code you can use it will stop you player rotate according to the directions

 private void InputManager()
    {
        float hDir = Input.GetAxis("Horizontal");

        if (state != State.hurt)
        {
            if (!anm.GetCurrentAnimatorStateInfo(0).IsTag("attack"))
            {
                if (hDir < 0)  // Go left
                {
                    rb.velocity = new Vector2(-speedX, rb.velocity.y);
                    this.transform.rotation = Quaternion.AngleAxis(180, Vector3.up);
                }
                else if (hDir > 0)  // Go right
                {
                    rb.velocity = new Vector2(speedX, rb.velocity.y);
                    this.transform.rotation = Quaternion.AngleAxis(0, Vector3.up);
                }
            }
        }
    }
0
votes

You need to add a bool Variable that is checking if the player is facing right, as well as a Flip Function that flips him:

bool bFacingRight = true;

private Rigidbody2D rb;

int _switch;

private void FixedUpdate() {
    InputManager();
}

private void InputManager()
{
    if (state == State.hurt || anm.GetCurrentAnimatorStateInfo(0).IsTag("attack"))
       return;
    
    float hDir = Input.GetAxis("Horizontal");

    // Move the character by finding the target velocity
    rb.velocity = new Vector2(hDir * 10f, rb.velocity.y);

    if (hDir < 0 && bFacingRight) { // Flip Left
       _switch = 0;
       Flip(_switch);
    }
    else if (hDir > 0  && !bFacingRight) { // Flip Right
       _switch= 1;
       Flip(_switch);
    }
}

private void Flip(int swth)
{
    // Switch the way the player is labelled as facing.
    bFacingRight = !bFacingRight;

    // Multiply the player's x local scale by -1.
    switch(swth)
    {
       case 0:
       this.transform.rotation = Quaternion.AngleAxis(180, Vector3.up);
       break;
       case 1:
       this.transform.rotation = Quaternion.AngleAxis(0, Vector3.up);
       break;
    }
}