- Home /
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:
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!
Answer by Bunny83 · Jun 29, 2016 at 06:29 PM
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.
Following up on the noncommutative nature of Quaternion multiplication, just as a relative rotation can be multiplied by another:
transform.rotation = Quaternion.AngleAxis(45, transform.up) * transform.rotation;
the same result can also be found by reversing the equation and replacing the relative axis with an absolute one:
transform.rotation = transform.rotation * Quaternion.AngleAxis(45, Vector3.up);
(Of course, this essentially just matches transform.Rotate())
Thank you Bunny83 and $$anonymous$$haon. Both explanation and examples were very helpful. I needed to rotate a camera on update around it's local up axis but the rotation had to be relative to the initial rotation, not cumulative. I ended up saving the initial rotation and multiplying it with a relative rotation around Vector3.up.
Quaternion initialCameraRotation = camera.rotation;
void Update()
{
transform.rotation = initialCameraRotation * Quaternion.AngleAxis(desiredRotation, Vector3.up );
}