When you work with physics in Unity you will eventually stumble upon Quaternion class. Official Unity documentation states:
Quaternions are used to represent rotations.
But behind those simple words lies a complex mathematical structure based on numbers and four-dimensional space (X, Y, Z, W).
First, let me tell you how I found out about Quaternions.
Those orange objects are spinning. When spinners are hitting player (red ball), most of the time player goes through objects, which is not what I wanted. The reason was that spinners had no Rigidbody, hence no physics.
void Update()
{
transform.Rotate(spinX, spinY, spinZ);
}
I had this simple transform.Rotate line. I found out that my issue is called Tunneling. Spinner was rotating too fast and since Unity is not checking collision every second, but every frame, the following bug happened:
- First frame: Spinner was in front of the ball
- Second frame: Spinner was behind the ball
To fix this issue, I was introduced to Quaternions.
Now back to Quaternions. Humans easily understand Euler Angles, that is, our beloved X, Y and Z. We can see them in Unity Inspector. But Euler Angles can have a bug called Gimbal Lock.
Imagine you have three rings nested inside each other like a gyroscope:
The outer ring rotates the object left and right (Y axis).
The middle ring rotates the object up and down (X axis).
The inner ring tilts the object sideways (Z axis).
They work in turns, dependent on each other. If you rotate the middle ring exactly 90 degrees, the inner ring will rotate and align itself with the outer ring.
At this point, the Z and Y axes are aligned. Now, whether you rotate the outer ring or the inner ring, the object performs exactly the same motion. However, it can no longer physically rotate in the third direction, it’s stuck. To regain the lost axis, you’ll first have to rotate the middle ring back.
For a human in real life, this isn’t a problem, but for Unity’s Euler angle math, it’s a disaster, physics breaks and loses control of one of the axes, and the object starts to jerk wildly or rotate in the wrong direction.
To resolve this issue, Quaternions were introduced. A Quaternion describes a rotation not as a sequence of three separate rotations, but as a simultaneous calculation of a direction vector and the angle of rotation around it using four components: X, Y, Z, and W.
Because of a four-dimensional structure, Quaternions have three advantages:
- No Gimbal Lock — object will rotate freely and correctly in any direction.
- Effective Interpolation — methods like Quaternion.Slerp allows calculating smooth transitions from one rotation to another.
- Performance — Quaternions allow the physics engine to work faster and better.
To fix the issue I wrote this line of code:
Rigidbody rigidBody;
void Awake()
{
rigidBody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
Quaternion deltaRotation = Quaternion.Euler(
new Vector3(rotateX, rotateY, rotateZ) * Time.fixedDeltaTime
);
rigidBody.MoveRotation(rigidBody.rotation * deltaRotation);
}
Since we work with physics, we use FixedUpdate and Time.fixedDeltaTime.
There we call Quaternion class and use its static method to pass three dimentional vector, and multiply it by Time.fixedDeltaTime. Eventually, we call MoveRotation on our Rigidbody and rotate our object.
You might ask what’s the difference between Time.deltaTime and Time.fixedDeltaTime.
Time.deltaTime — is the difference between normal frames in Update function. It always changes based on PC stats. Stronger the PC, less time there is between frames.
Time.fixedDeltaTime — its fixed time between frames in FixedUpdate function. In Unity it is always the same, 0.02 seconds.
In a nutshell, deltaTime is dependent on game lag and FPS, while fixedDeltaTime is a stable, perfectly smooth metronome for the physics engine.
So let's just not manually intervene with Quaternions and just use given static methods of Quaternion class that are basically our translators.
To sum up, Quaternions in Unity are not a replacement for Euler angles, but rather a safe internal implementation of them. We write code and configure the inspector in degrees, but the engine converts them into Quaternions behind the scenes, so that collisions, triggers, etc., are processed smoothly, quickly, and without errors.

Top comments (0)