Quaternion Rotation - AngleAxis/EulerAngle

I ran this simple script to try to grasp rotation manipulation. I’ve read tons of forms, so I kind of have an idea how things are done, but his straight up doesn’t make sense.
I was under the impression AngleAxis rotations would basically stack that way you could preform rotations in a specific order. That doesn’t appear to be the case, but I have to be doing something wrong.

if (Input.GetKey(KeyCode.Space))
            transform.localRotation = Quaternion.AngleAxis(45, Vector3.forward);
            transform.rotation = Quaternion.AngleAxis(90, Vector3.right);

I’d like to assume after this runs the cube would have the orientation of (90,0,45), but it’s just (90,0,0). Why?
Is there any way to get that effect?

Original orientation cube was standing up on end

Next I ran this code bit, and it also doesn’t seem to do what’s intuitive.
It appears the cube rotated (90, 45, 0). When running the code with x and y switched we reach the exact same effect.

if (Input.GetKey(KeyCode.Space))
            transform.localRotation = Quaternion.Euler(90, 0, 45);

Original orientation cube was standing up on end

In conclusion when running any of these rotation commands separate the outcome I expect is indeed the result, but when you change more than one Axis… forget it. An explanation or pointer towards some reference would be perfect.

localRotation and rotation describe both the same rotation, just in different coordinate spaces if the object actually is a child of another object. If there’s no parent object then localRotation and rotation are exactly the same thing. When you set the rotation / orientation of an object you overwrite the old orientation with the new one.

If you want a relative rotation you have several options:

  • First the simplest one is to use the Rotate method. It allows you to perform a relative rotation on an object either based on the axis aligned relative euler angles, or by specifying a rotation axis and a relative angle. Note that the Rotate method has an optional parameter to specify the coordinate space in which you want to rotate (world space or local space).
  • Another way is to use quaternion multiplication. You setup a quaternion rotation and muliply the objects current rotation by that quaternion

Something like that:

transform.rotation *= Quaternion.AngleAxis(90, Vector3.right);

If you want to know more about quaternions have a look at this numberphile video and / or this 3b1b video

Honestly I don’t know anything about Quaternions though this might be helpful, Unity - Scripting API: Quaternion.Euler