unity3d Intro to Quaternion vs Euler


Euler angles are "degree angles" like 90, 180, 45, 30 degrees. Quaternions differ from Euler angles in that they represent a point on a Unit Sphere (the radius is 1 unit). You can think of this sphere as a 3D version of the Unit circle you learn in trigonometry. Quaternions differ from Euler angles in that they use imaginary numbers to define a 3D rotation.

While this may sound complicated (and arguably it is), Unity has great builtin functions that allow you to switch between Euler angles and quaterions, as well as functions to modify quaternions, without knowing a single thing about the math behind them.

Converting Between Euler and Quaternion

// Create a quaternion that represents 30 degrees about X, 10 degrees about Y
Quaternion rotation = Quaternion.Euler(30, 10, 0);

// Using a Vector
Vector3 EulerRotation = new Vector3(30, 10, 0);
Quaternion rotation = Quaternion.Euler(EulerRotation);

// Convert a transfroms Quaternion angles to Euler angles
Quaternion quaternionAngles = transform.rotation;
Vector3 eulerAngles = quaternionAngles.eulerAngles;

Why Use a Quaternion?

Quaternions solve a problem known as gimbal locking. This occurs when the primary axis of rotation becomes collinear with the tertiary axis of rotation. Here's a visual example @ 2:09