Intro to Quaternions & Euler Angles

Alex Tolson
3 min readDec 23, 2023

--

Big Words…

Quaternions are simply how Unity understands rotations.

Euler Angles: (Euler: pronounced: oiler) — This is how rotations are presented to us, the developers.

45 Degrees on the X axis.” is equivalent to saying, “X axis has an Euler angle of 45”.

Setting an Euler angle of 45 on the X axis.

Using the Rotation tool:

The Rotation tool.

Visualizes the Euler angles or the degree of rotation along an axis.

Visualization of Euler angles in the scene view in Unity.

This is an effective way to present information for both the Unity program and developers.

Now, Quaternions are similar to our degrees of rotation, save for one detail. They have an X, Y, Z, and W degree of rotation. Unity, instead of requiring developers to understand the W degree of rotation, converts the Quaternions to Euler angles for developers to understand. The individual rotations along the x, y, and z axes are the Euler angles along those axes.

When dealing with Euler angles, it’s recommended that we always compute our rotations through Quaternions. Remember: this is how Unity processes angles and degrees.

It’s okay to use Rotation tool in the Unity scene view. However, we do not want to directly modify rotations through Euler angles through code, because of a sometimes-occurring error named Gimble Lock.

Gimble Lock happens when (in a 3-dimensional, 3x-Gimble mechanism) there is a loss of 1 degree. The result is the Y Euler angle and the Z Euler angle may align, causing a “lock” and breaking the rotation.

This can be avoided by computing the rotations through Quaternions. Unity will then present the Quaternion as an Euler angle.

So, what’s Quaternion.identity?

Quaternion.identity is the same as setting the Euler angles for the X, Y, and Z to zero.

The Instantiate tool tip explains that Quaternion rotation needs to be set.
Instantiating an enemy as Vector.zero for position and 0,0,0 on the X,Y,Z for rotation.

Don’t forget to set the prefab in the Inspector:

Completing the reference to the Prefab we will be instantiating.

Let’s test out our script:

Enemy instantiated with zero rotation.

If we wanted to instantiate a rotation:

Computing our rotations through a Quaternion.

The result:

Let’s take a look at our transform for the enemy:

This concludes the Intro to Quaternions & Euler Angles. In the next article, we will talk about other ways to compute Euler angles through Quaternions.

Thanks for Stopping By.

--

--