Monday, February 20, 2017

Rotation in Unity

Unity's transform system can be bewilderingly complex to even those experienced with other 3D software, and even with such a strong community, straightforward explanations can be hard to find. I just spent an entire morning trying to solve a specific problem regarding a rotation transform, and thought it would be good to share with everyone.

First, some basic facts about unity rotation and axes systems:
  1. There is a global axes system which is always fixed. The vector (1,0,0) defines the right direction, (0,1,0) defines up, and(0,0,1) defines forward in this system.
  2. Every object also has a local axes system. For simplicity, let's assume that this object has no parent (i.e., it is at the top of the hierarchy in the scene). Then:
    • Its local axes will coincide with the global axes if it has no rotation.
    • Its local axes move with the rotation such that the z axis of local axes system has the direction: rotation * (0,0,1). The other axes are defined in the same way. This helps you find the forward direction of the object easily, and is referred to in code by the variable transform.forward.
  3. To confuse things further, we have global rotation and local rotation. Global rotation (referred to by the variable transform.rotation) is the rotation of the object from its "original model rotation", i.e., rotation applied to the original model. This is also called rotation as seen in the world space.
  4. Local Rotation (referred to by the variable transform.localRotation) is the rotation of the object relative to the rotation of the parent's rotation. This means, that the following is an identity:

    transform.rotation == transform.parent.rotation * transform.localRotation

    Note that the word "local" in localRotation has nothing to do with the word "local" in the local axes system. All rotations are defined in the global axes system. This specific point confused me for a while.
The toolbar gizmo controls let you see the current directions in the local and global axes systems. The screenshots below show the difference between the two axes systems.

Global axes
Local Axes
Now, back to the problem I was facing. I'm working on a VR game where the player uses tracking controllers to grab a sniper gun and aim. I have the sniper rifle mounted on a bipod, and its rotation while attached to the bipod is defined by the hand movement.

The problem I faced was that when I used the hand's movement to calculate a rotation and applied it to the gun, the gun rotated around the axis aligned with the barrel. The pictures illustrate the problem, and it becomes disorienting for the player to try and aim with a gun rotated in this way.


I need to tilt the head to see through the scope!


Properly oriented
To solve this, we need to understand a physical interpretation of a Quaternion. A quaternion defines a rotation of rotation angle around an rotation axis, as shown in the diagram below.




To prevent the gun from rotating around the barrel axis, we can simply remove the component of Quaternion's rotation axis along the barrel axis. Confusing? The code below might help:

float rotAngle;
Vector3 rotAxis;
rotation.ToAngleAxis(out rotAngle, out rotAxis);
rotAxis = rotAxis - gun.forward * 
    Vector3.Dot(rotAxis, gun.forward);
gun.rotation = Quaternion.AngleAxis(rotAngle, rotAxis);

In above code, gun.forward is aligned with the barrel axis, and the vector dot product calculates the component of rotation axis along gun.forward. Also note that I am setting the final rotation to the gun.rotation, which is because a) the rotation was calculated using objects in world space, and b) the vector gun.forward gives us the gun's barrel axis direction in the world space.

I hope this helps you understand rotation in Unity a bit better.

P.S. Make sure that all your models are such that the local forward direction is along the logical front of the model (e.g., for a gun, transform.forward aligns with its barrel). This helps immensely in simplifying code, and also allowing you to reuse scripts on multiple objects. If this is not already the case with a model, it likely needs to be edited in a 3D modeling software.