Problem using Quaternion.AngleAxis around transform.up

Hello boys and girls

I am missing something fundamental about Quaternions and can’t find the answer on the forums.

Here is my setup:
73124-screen-shot-2016-06-28-at-224953.png

I have two identical game objects

The bottom one is rotated so that local up points to the left, the top one is not rotated

Both objects have the same component with one line of code on Start

void Start () 
    {
        transform.rotation = Quaternion.AngleAxis(45, transform.up);
	}

The top object rotates around it’s up axis as expected.

The bottom object rotates so that it’s up axis changes. I don’t understand why. I expect it to rotate around the local UP axis. I know that transform.up is the local up axis in world coordinates so I think it should work.

What am I missing? Please help!

You got something fundamental wrong here. Quaternions actually don’t specify an “absolute” rotation but a relative. Unity simply defines the default orientation as z=forward y=up and x=right.

When you use Quaternion.AngleAxis you create a “relative rotation” which can rotate a vector or a coordinate system from an initial state by that relative amount. However you don’t do a relative rotation but you set the “global” rotation of the object to that rotation. So you basically rotate the object from it’s initial (0, 0, 0) state by the relative amount which is not what you expect.

To rotate the current orientation by that relative amount you have to multiply the current rotation by that relative amount:

transform.rotation = Quaternion.AngleAxis(45, transform.up) * transform.rotation;

Note: Quaternion multiplication is not commutative, so the order of it’s operands matters.

However it’s usually easier to use the Rotate method :

transform.Rotate(0f, 45f, 0f);

or

transform.Rotate(Vector3.up, 45f);

or

transform.Rotate(transform.up, 45f, Space.World)

All those 3 variants do the same thing as the AngleAxis variant i’ve posted.