- Home /
Question by
unityExp · Jul 30, 2013 at 09:36 PM ·
c#rotationmovement3rd person controller
Set Character's Rotation
Hi guys,
I'm developing a learning game and what I thought would be one of the simplest things is turning out to be a nightmare. Observe the following code:
if (Input.GetKeyUp(KeyCode.W))
{
// set its local Y rotation to 0f or 360f (the model is facing forward)
}
else if (Input.GetKeyUp(KeyCode.D))
{
// set its local Y rotation to 90f
}
else if (Input.GetKeyUp(KeyCode.S))
{
// set its local Y rotation to 180f
}
else if (Input.GetKeyUp(KeyCode.A))
{
// set its local Y rotation to 270f
}
I have tried transform.rotate, Quaternion euler angles, and pretty much every code of snippet in Unity Answers. It either keeps rotating it relatively by the value every time the same key is pressed or doesn't rotate at all. Here my character is supposed to constantly move forward in its Z axis, and this quick snap rotation is supposed to sets is movement direction.
Comment
Answer by Arkuni · Jul 30, 2013 at 10:16 PM
Have you been trying to do the following?
transform.rotation.x = x; transform.rotation.y = y; transform.rotation.z = z;Because that annoyed me to hell and back as well. For some reason, you can't edit the xyz values. You have to create a new vector and apply it, like this:
transform.rotation = Quaternion.Euler(new Vector3(xDegrees, yDegrees, zDegrees));xDegrees, yDegrees and zDegrees can be replaced by any number between 0 and 360 (even though it would problably wrap around itself just fine).
thank you so much this line of code just made my day transform.rotation = Quaternion.Euler(new Vector3(xDegrees, yDegrees, zDegrees));
Your answer
![](https://koobas.hobune.stream/wayback/20220613111228im_/https://answers.unity.com/themes/thub/images/avi.jpg)