Rotate Character 90 Degrees (694278)

I’m trying to make my character rotate 90 degrees each time a player presses an arrow key, but I’m having trouble with the rotation. Currently, the player moves extremely quickly between -90, 90, and 180. My code is below. if anyone can help me understand why this approach isn’t working and what would be a better approach, I’d greatly appreciate it.

public class PlayerMovement : MonoBehaviour {

    void Update()
    {
        float h = Input.GetAxisRaw ("Horizontal");

        if (h != 0)
        {
            Turn ();   
        }
    }

    void Turn ()
    {
        Vector3 angles = transform.eulerAngles;
        float y = angles.y + 90;
        transform.rotation = Quaternion.AngleAxis(y, Vector3.up);
    }
}

Try using GetKeyDown or GetButtonDown instead of GetAxis. That will only return true for the first frame its ‘down’. :slight_smile:

Also, I’d recommend that you not use euler angles that way. You can do:

transform.rotation *= Quaternion.Euler(0,90,0);
3 Likes

Thanks so much for your help! The problem was indeed caused by the GetAxisRaw.