Understanding Movement and Rotation in C#

Directional Movement

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

In this video segment, learn about player input and how to link it with objects’ movement and rotation.


  • Player Input
  • Link input
  • Rotation
  • Movement
  • Physics system
  • FixedUpdate
  • Quaternions

About this video

Alan Thorn
First online
12 January 2019
Online ISBN
Copyright information
© Alan Thorn 2019

Video Transcript

[Audio Begins] [0:00:00]

Alan Thorn: In this movie, we’re going to look at how to link player input such as keyboard button presses or gamebat button presses. We’re going to learn how to link player input to movement and rotation of objects inside of the scene. Specifically, we’re going to achieve two things. We’re going to link player input to movement of an object, such as the movement of the car object. And we’re going to link input to rotation, so that we can both move and rotate object on the basis of player input.

To get started at this project, I’m going to begin in our low poly scene. Here we have our car, and we have this garden and the driveway. I’m going to be linking the input, the controls that we press, to the movement and rotation of this car object. Now the idea here is not necessarily to create believable car physics, that is beyond the scope of this course. But what I will be demonstrating is how to create motion, rotation, and movement. Realistic and sensible movement on the basis of player input. Let’s get started at doing that.

I’m going to enter the scripts folder and create a new script file by right-clicking and choosing Create, choosing C# script, and I’ll choose Player Controller. And I’m going to drag and drop the player controller script onto the car root object here inside the hierarchy panel to add that to the scene. I’ll double-click on the Player Controller script to open that inside visual studio. Now there’s some interesting properties we need to add to this to get it to function as we intend. First of all, we’ll need a movement speed for the player character. So I’m going to choose public, float, and I’ll choose move speed. And this move speed is defined in terms of meters per second. So right now, this is going to move forwards by 1 meter per second.

The next thing I’m going to do is to create another variable. That’s simply going to be public, float, and I’ll call it rotspeed, which effectively is going to be the rotation speed in terms of degrees per second. I’ll minimize the script file here and allow the code to compile. There it goes ahead and compiles. And on compilation, you’ll see from the inspector here that we have both movement speed and rotation speed, which we can tweak from the object inspector.

Next, I’m going to go back. In fact, what I’m going to do is I notice this syntax highlighting here inside visual studio. That isn’t really working at what it should do. Now, whenever you get this, you simply can close visual studio, and then load it back up so that Unity will recompile and update all of the settings of those script files. I’ll resume recording when I’ve done that.

I’ve now reloaded visual studio, and the syntax highlighting is now correctly updated to reflect the validity of the code. So I’ve gone ahead and created these two variables. In addition to this, I need to get a reference to both the transform component and to the rigid body component attached to the car object. So we’re going to have a transform variable, ThisTransform. And we’re also going to have a rigid body variable which references the rigid body attached to the object. Inside the awake function, I’m going to retrieve a reference to these two objects, so ThisTransform equals get component. And I’m going to get the transform component. In addition, I’m going to do the same thing for the rigid body, get component. Having now retrieved both of these components, I then want to update the input here inside the update function. I’m going to be doing this inside the fixed update function because when working with the physics system, you want to make sure that you’re working inside the fixed update function so that everything you’re doing is happening in synchronization with the physics system. You’ll notice here, in fact, that actually I have an additional semicolon. I’m going to close that. Inside the fixed update function, the first thing that I want to do is read input from the player. There are two ways we can do this. We can read vertical input, that is input up and down, which is going to be controlled by the up and down keys on the keyboard or the W-S keys on the keyboard. And there’s horizontal input, which is left and right motion. A and D on the keyboard, or left and right. We can read that quite easily inside Unity by using the input.getaccess function here. And then I can specify the axis that I want to read. In this case, it’s the horizontal axis. Now, this is going to return back of value of between negative one and one. If we’re pressing the left key on the keyboard, then we’re going to get a value of negative one. If we’re pressing the right key on the keyboard, we’ll get a value of positive one, and if we’re pressing nothing, then we’ll get a value of zero.

I’m going to do the same thing here for the vertical axis as well. So I’m going to choose vertical and retrieve that value here inside the vertical field. So effectively, these two floating point variables are going to control the input. So let’s start by controlling the movement of the player character.

I’m going to use the rigid body dot velocity to control this. So our velocity here is going to equal the forward vector multiplied by our speed, which is our movement speed in this case, and in addition, I’m going to be multiplying that by the vertical value. And remember what vertical does. It’s simply up and down. So motion of the character is going to be controlled by our vertical input. Now you’ll notice here that because vertical, if we’re pressing nothing is going to be zero. But ultimately our forward vector multiplied by its move speed is going to be multiplied by zero, which means no movement at all. So that’s why this line works.

In addition to this, I want to control the rotation of the player character by using the horizontal field. I’m going to choose ThisTransform.Rotation, and in this case multiplies equal. And I’m going to use quaternion dot, and in this case I want to rotate around the Y axis, so I’m simply going to choose euler angles here. So I’m going to use euler, and I’m going to choose – notice there are several versions of this function. One for vector three, and one for simply three floating point values. I’m going to use the floating point value version. I want to rotate in the Y field, so I’m going to rotate by our rotation speed, multiplied by Time.deltaTime, multiplied by the horizontal value, like so. And then I’m going to save this code here. On saving this, I’m going to minimize the code file, and go back to the scene, and press the Play button on the toolbar.

Now when I do this, you can see that by holding down the up key, I’m moving forward. And when I hold it down, I’m moving back. And then I can use the D key to rotate left or to rotate right. So that potentially, I can move in any direction that I want. Like so. So I can move around and turn completely in opposite directions and move the car that way.

So in this movie, we’ve seen how to code player input, and how that can be used to control movement and rotation. In this movie, we coded a movement system so that we linked player input, that is up, down, left, and right on the keyboard. The horizontal and vertical axis to the movement and rotation of an object. We can move them using vectors and rotate them using quaternions.

[Audio Ends] [0:08:30]