- Home /
How can i rotate my player using Input.GetAxis?
I am trying to make my player rotate on the y axis. I want him to be able to rotate 90 to the right when the user Input.GetAxis("Horizontal") > 0 and rotate 90 left when the Input.GetAxis("Horizontal") < 0 and to rotate to 180 when Input.GetAxis("Vertical") < 0.
The script im using is:
private Quaternion targetRot;
void Start ()
{
targetRot = Quaternion.Euler (0, 0, 0);
}
void Update ()
{
if (Input.GetAxis("Horizontal") < 0)
{
targetRot.y = -90;
}
if (Input.GetAxis("Horizontal") > 0)
{
targetRot.y = 90;
}
if (Input.GetAxis("Vertical") < 0)
{
targetRot.y = 180;
}
transform.rotation = targetRot;
}
I dont understand why this wont work. Can anyone help me? Also my next step is to make it where the player has rotation in between 90 intervals such as if the user inputs Horizontal and Vertical at the same time. Can anyone tell me how to fix this. Thank you
Answer by zach-r-d · Jul 13, 2015 at 09:02 PM
Quaternions are tricky and unnecessary for this use case, so I recommend using Euler angles instead. Just do the following:
Change targetRot to be a Vector3 instead of a Quaternion.
Delete line 5 (or set targetRot to
new Vector3(0,0,0)
).Change line 23 to
transform.eulerAngles = targetRot;
Answer by dabo1 · May 27, 2016 at 12:14 PM
You could also use transform.Rotate(Vector3.up)
for example
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
A constant speed rotation finishing with a correct angle. 2 Answers
Quaternions acting up 1 Answer