Search code examples
c#unity-game-enginequaternions

Why does Axis not change when they are on 0f?


I've been trying to understand rotations via Quaternions but I got a little bit confused about a topic.

void Update() 
{
    transform.LocalRotation = Quaternion.Euvler(15f, 0f, 0f);
}

Why doesn't the y and z rotations set back to 0?

I thought since the code was in the Update it should keep reset the rotations to 0


Solution

  • When you set the Euler angles to (15, 0, 0), you are setting a rotation of 15 degrees around the x-axis. One important thing to note about Euler angles is that they are applied in a specific order, which is commonly referred to as the "rotation order" or "Euler order". In Unity, the default rotation order is "XYZ", which means that rotations are applied in the order of X, then Y, then Z. So, in your example, when you set the x-rotation to 15 degrees, it rotates the object around the x-axis by 15 degrees. However, since the y and z rotations are not explicitly set, they retain their previous values. So, if the object had any non-zero y or z rotation before the Update() function was called, it would still have that rotation after the function is called. If you want to set the y and z rotations to zero while setting the x-rotation to 15 degrees, you can create a new Quaternion rotation using Quaternion.Euler(15f, 0f, 0f) and then set the object's local rotation to that new rotation, like so:

    void Update()
    {
    Quaternion newRotation = Quaternion.Euler(15f, 0f, 0f);
    transform.localRotation = newRotation;
    }
    

    This will create a new Quaternion rotation that only has a 15-degree rotation around the x-axis, and then set the object's local rotation to that new rotation, effectively setting the y and z rotations to zero.