Understanding Movement and Rotation in C#

# Quaternions

Your browser needs to be JavaScript capable to view this video

In this video segment, learn the difference between Euler angles (i.e., degrees) and Quaternions and how to use it to rotate objects inside Unity.

## Keywords

• Rotation
• Unity
• Euler angles
• Quaternions

Author(s)
Alan Thorn
First online
12 January 2019
DOI
https://doi.org/10.1007/978-1-4842-4442-5_4
Online ISBN
978-1-4842-4442-5
Publisher
Apress

## Video Transcript

[Audio Begins] [0:00:00]

Alan Thorn: In this movie, we’ve going to consider the important subject of rotations. Previously, we made objects move by changing their position. But position is not the only thing we can change on an object that fundamentally changes the way it is positioned or moved within the scene. Another critical influence over the movement and alignment of objects is rotation. Specifically, in this movie, we’re going to explore how we can rotate an object. And the difference between euler angles, that is degrees, and quaternions.

I’m going to begin with the scene we’ve been working with, that is the driveway scene here. You can see the car parked on the driveway. If I select the car root object which was created inside an earlier movie, you can see that we have a mover script attached to that car. The mover script will maneuver or transform the object along its forward axis. And it also takes into account the physical obstacles inside the level. I’m going to temporarily deactivate the mover script so that the object no longer moves, and I can concentrate and focus on rotation. If I select the car root object, you can see from its transform component that we have a rotation field. And we can actually express three values here. That is X, Y, and Z. X, Y, and Z represent the main rotation ingredients for this car to define it’s orientation in the level. In particular, we can rotate on the X axis by click and drag on the X. You can see that the car rotates around the X axis here. This type of rotation is known as pitching. I’m going to reset that back to zero.

We can also rotate around the Y axis, which is the axis sticking out of the top of the object. This is known as yaw, that’s Y-A-W. I’m going to set that back to zero. And in addition, we can also rotate around the zed axis here. That is the forward axis pointing out of the front. This kind of rotation, rotation around the forward axis is known as roll. We have pitch, yaw, and roll. These are the three main types of rotation that can occur. In Unity, at least in the inspector, these rotations are defined in euler angles, that is degrees where one complete turn or revolution is 360 degrees. A half turn is 180 degrees, and a quarter turn is 90 degrees. And half of that is also 45 degrees. We have these different car orientations that we can use to define the rotation of an object. But how can we define that inside our script files. The moment that we start working with rotations and alignment inside our script files, we’ll see that under the hood, Unity, in fact, doesn’t use euler angles at all. Instead, it uses a different form of angle measure known as a quaternion. I’m going to show you how these work.

I’m going to go to the project panel. Right-click, choose Create, and then choose C# script. I’m going to call this script the rotator, or rotator, and drag and drop that onto the car root object inside the scene to attach the component to the root object. By selecting car root, I’m just going to confirm the script file has been attached, and it has. I’ll double-click the script file to open that inside visual studio here to bring up our rotator script. This rotation script is simply going to allow us to specify or change the rotation of an object. In the later movie, in fact, in the next movie, we’ll see how we can animate the rotation of an object. But in particular, I want to define the rotation of an object using quaternions. Here’s how we can do that.

First of all, I’m going to create a local variable to give access to the transform component attached to the object. The reason I’m doing that is because the transform component has a rotation property allowing us to define the orientation of an object. I’m going to choose private, transform, this transform. And that’s before I’m going to assign that a value inside the awake function when the level begins. When the level begins, I’m going to get access to the transform component. I choose get component, and then I’ll simply specify transform to get the transform component.

Inside the update function, I want to set the orientation of this object to rotate 90 degrees around the Y axis. Now there’s no real easy way to do that using euler angles. We could, in fact, choose this transform dot rotate. And if I choose rotate, it allows me to specify degrees here around which we can rotate. But the rotate function will spin the object. Instead, what I want to do is I want to set the rotation variable. Which you’ll notice the rotation variable, in fact, is not simply a degree specification. It’s not even a vector. It is, in fact, a quaternion object. So in order to define this, we have to use a quaternion structure. And here’s how we can define a quaternion and build that structure from three euler angles. I’m going to choose quaternion dot, and there is a function, a static function here of euler. If you take a look at the argument list, you can see that the inputs for this are float X, float Y, and float zed. These are three angle measurements in degrees. So I can build a complete quaternion by specifying X, Y, and zed rotation.

I’m going to choose zero for the X rotation, 90 degrees for the Y, and zero for the zed rotation, meaning that the object is effectively is only going to rotate on the Y axis. I’m going to choose Command-S to save that code, minimize the script, return back to Unity, and test out this code by pressing Play on the toolbar. When I press Play on the toolbar after the script has been compiled. The car will rotate on the Y axis. If I select the car root object, you can see here from the field inside the transform component that it’s being rotated by 90 degrees and is now oriented in a completely different direction. Notice, this is not rotating the car 90 degrees every single frame. Rather it is setting the Y rotation of the car to 90 degrees on every frame. I’m going to stop playback here. So here we’ve seen how we can rotate an object using quaternions.

In this movie, we explored how to rotate an object using quaternions. These are a substitute that Unity uses in place of euler angles. Euler angles define angles in terms of traditional degrees. Instead, quaternions can take degree inputs. But under the hood it creates a very different kind of mathematical structure that solves important problems regarding rotation. It solves the problem of gimble lock. But in any case, quaternions can be effectively used to rotate and orient objects inside of Unity. Quaternions, you need to make them your friend if ever you want to rotate, turn, or orient and realign objects inside a Unity scene.

[Audio Ends] [0:07:40]