- Home /
Smooth rotation about global axis instead of local axis.
I am trying to rotate a game object 90 degrees about the global axis but can only seem to make it rotate about it's local axis.
I am calculating rotation like this and then slerping it after:
if (direction == directions.right)
{
newRotation = transform.rotation * Quaternion.Euler(0,-90,0);
}
else if (direction == directions.left)
{
newRotation = transform.rotation * Quaternion.Euler(0, 90, 0);
}
else if (direction == directions.up)
{
newRotation = transform.rotation * Quaternion.Euler(90, 0, 0);
}
else if (direction == directions.down)
{
newRotation = transform.rotation * Quaternion.Euler(-90, 0, 0);
}
Cheers!
Answer by robertbu · Aug 12, 2013 at 06:16 AM
To rotation about the global axis, reverse the order the Quaternions are combined:
if (direction == directions.right)
{
newRotation = Quaternion.Euler(0,-90,0) * transform.rotation;
}
else if (direction == directions.left)
{
newRotation = Quaternion.Euler(0, 90, 0) * transform.rotation;
}
else if (direction == directions.up)
{
newRotation = Quaternion.Euler(90, 0, 0) * transform.rotation;
}
else if (direction == directions.down)
{
newRotation = Quaternion.Euler(-90, 0, 0) * transform.rotation;
}
Thank-you so much! Can you by any chance explain why it works this way?
Each individual Quaternion rotation is an absolute rotation around world axes. There is no 'local' rotation to the local axes. So find a cube object, label front, top and right. Pick something interesting for a value of newRotation like (45, 45, 0). Start with the cube in a (0,0,0) rotation, then apply the two rotations above. That is first rotate one of the values above such as (0,90,0). Then apply the newRotation on the world axes to your cube. You will see in this order the cube appears to move locally. Bring the cube back to (0,0,0), and reverse the order. Start with with the newRotation then apply the Euler rotation from above. This will appear to be a 'global' rotation.