Understanding Movement and Rotation in C#

# Rotate Objects

Your browser needs to be JavaScript capable to view this video

In this video segment, learn how to rotate an object over time at a consistent speed.

## Keywords

• Rotation
• Speed
• Quaternion multiplication
• Combined rotation

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

## Video Transcript

[Audio Begins] [0:00:00]

Alan Thorn: In this movie, I’m going to focus on how to rotate objects over time. Previously, we saw how we could turn an object to face a particular direction by rotating the X, Y, or zed component of the rotation field. In this movie, we’re going to focus specifically on one, how we can rotate an object, and two, how we can change that rotation smoothly over time at a consistent rotation speed. To get started I’m going to take the scene that we have here. This is the car on the driveway scene. And previously, we’ve been building up the rotator script that you can find here inside the project panel. I’ll double-click on the rotator script to move to the script that we created in the preceding movie.

There we used the rotation field to specify the rotation on the Y axis. This is 90 degrees. That is specified 90 degrees in euler angles, but what Unity is doing from the quaternion.euler function is constructing a quaternion to represent that orientation. Here what I want to do is to change the value of this field over time, and I’m going to do that by using quaternion mathematics. Let’s see how that works.

I’m going to comment out the rotation field here and instead, I’m going to begin the construction of a completely new statement. It’s going to begin with the same as before, that is this transform.rotation to specify the rotation here, but I want to do is to gradually rotate the object by a specified rotation speed over time so that the object spins around a specified axis. In this case, I’m going to move up here and add a new variable. I’m going to choose private, and this time I’m going to choose float, and I’ll choose rotspeed to name that variable. And the rotation speed is going to be set to 90 degrees, that is 90 degrees per second. Down here in the rotation statement, I’m going to use the multiplication symbol, that is multiplication equals here. And the reason for that because by combining quaternions together we do not use the plus symbol as we would do with positions. To add together two quaternions, that is to combine the two rotations of two quaternions, we have to, in fact, multiply them together. So in this case, I’m taking the rotation of our current object and to apply a new rotation to it, I need to apply or multiply a new quaternion to it. In this case, I’m going to specify the quaternion that we’re going to be using for the rotation.

Here I’m going to choose quaternion dot, and I’m going to use the static function angle axis. This allows us to specify an angle around which we want to rotate or an axis and the amount of rotation around it. In this case, I want to rotate 90 degrees, or to continue to rotate 90 degrees per second around the up axis. So I’m going to specify here the rotation speed like so, and I’m going to multiply that by Time.deltaTime. Remember, we need to multiply it by Time.deltaTime to create consistent rotation. And then finally, we want to rotate around the Y axis. That is the upward pointing axis. I can easily get access to that by using the transform component and choosing transform.up to rotate around that axis. I’m going to use Ctrl-S here to save that code, minimize visual studio, return back to Unity here. I’ll select the car root object, and from the object inspector here when the code compiles, that is in fact that under the rotator script, I can in fact see the speed of rotation. I can easily fix that by jumping back to the rotator script and making sure that the speed is not private, but public. We’ll just Ctrl-S to save that, go back here, so that when I select the root object, I get the flexibility of being able to change the rotation speed directly here from the inspector. I’m going to choose Play on the toolbar. And when I do this you can see that the object is now rotating around its Y axis.

Of course, that’s still pretty fast. I can easily reduce that by say changing it to 30 degrees, and suddenly we have a much smoother rotation. Obviously, it’s still trying to take into account some of the surrounding physical obstacles. I can easily try to move that out of the way here to create a slightly better rotation for this object and choose Play to continue to rotate this around its central axis. And there now it’s rotating around the Y axis. Notice how this function is working. The function angle axis. It’s taking a vector three structure representing a direction or an axis around which we should rotate, and the first parameter simply specifies what that rotation is, taking into account Time.deltaTime.

In this movie, we saw how to smoothly rotate an object around the specified axis. By using the quaternion.angleaxis function alongside Time.deltaTime. We also saw that to combine quaternions together, we do not use the additive operator. Instead, quaternions need to be multiplied together in order to combine their rotations.

[Audio Ends] [0:05:45]