# How does Quaternion.AngleAxis work?

I want a cube to rotate around it’s local up-axis and I’ve been messing around with it but the results have been weird… definitely not what I was expecting.

My setup :
I have a cube whose orientation is NOT axis-aligned with the world’s axis. The following script is added to that cube :

``````    //rotate the cube on it's local y/up axis.
void Update()
{
transform.rotation =  Quaternion.AngleAxis( t, this.transform.up );
t++;
}
``````

and what happens is that the cube’s orientation aligns itself with the world’s axis and then starts doing the rotation. Is this expected?

To get the desired affect I have to do the following (rotate it back to the desired orientation afterwards) :

``````	void Update()
{
Quaternion previous = transform.rotation;
transform.rotation = Quaternion.AngleAxis( 1, this.transform.up ) * previous;
}
``````

1 Like

what is wrong with transform.Rotate(transform.up)? Unity - Scripting API: Transform.Rotate

Transform.Rotate() will do the operation you’re looking for, yes. But as to your original question, the “extra step” (as you describe it) is necessary given your approach. Take a look at your original code sample: it’s ignoring the original rotation entirely when it sets the new rotation. There’s literally nothing on the right-hand side of the “=” that makes any reference to the original rotation. So of course it’s not represented in the result you get.

Basically, your first thing is analogous to saying “rotation = t” in 2D, when what you want to say is “rotation = rotation+1.”

It just so happens that quaternions use an operation other than “+” to compose (it overloads the “*” operator because it’s mathematically an extension of the way multiplication works on complex numbers, but the details there don’t really matter). So the analog of the 2D operation is rotation = q * rotation (not rotation = rotation * q as I said before I edited this just now).

1 Like

Thanks for both of the replies. I wasn’t aware of the transform.Rotate method (just starting out with Unity), but regardless I would still like to get myself better acquainted with these APIs.

Anyway Tim, I have tried

``````transform.rotation *= Quaternion.AngleAxis( 1, this.transform.up );
``````

before and it doesn’t rotate on the desired axis. It kind of looks like it’s rotating on all of the axes.

1 Like

Ah, well, now I have to admit that I screwed up when I said “rotation * q.” What you really want in this instance is “q * rotation.” These two are not the same with quaternion multiplication (just as they are not in the case of, say, matrices), and in quaternion space it’s left-multiplication that composes a new rotation onto an existing one. So, oops.

That said, C-style syntax treats *= as right-multiplication, which is why it’s not working as intended in your example. Try

``````transform.rotation = Quaternion.AngleAxis( 1, this.transform.up ) * transform.rotation;
``````