Understanding Movement and Rotation in C#

Vectors and Positions

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 how to move objects forward using script inside Unity that can transform the position based on its velocity.


  • Script
  • Unity
  • C#
  • Mover
  • Vector
  • Game object
  • Position
  • Speed

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 focus on the critically important subject of how to make objects move using script files inside Unity. This is a subject that absolutely every Unity game play programmer will need to know. You’re going to want to know how can we move objects inside the scene. Specifically, we’re going to look at two major topics. The first one is how we can use the position variable of the transform component to change the position of an object, not just to a random arbitrary point, but to create motion over time. Secondly, we’re going to look at how we can use the idea of speed to make the object move at a particular speed over time.

To demonstrate the movement of this object, I’m going to take this low poly scene that you can see here inside Unity. It’s a low poly garden with a car located on a driveway here. Now this car object is this car here inside the hierarchy. It’s a completely separate mesh object. And we want to use this object to create motion to this car inside the scene. Now to do that we could use many different techniques. I could prescript the animation by using the animation tools in Unity to move the car from this location along here to this location. But I want to create this inside script because data gives us the flexibility to change the location that the car moves to. We can vary the speed over time and use various different game play elements to control the movement of the car.

So to get started at doing this, I’m going to switch back to the project panel and create a completely new script file. To do this I’m going to right click, choose Create, and then choose C# Script. And I’m going to name the script file to mover here. So this is called mover. Notice that I’ve used the very generic name of mover. I’ve not called this car mover. I’ve not called this tree mover or character mover. I’ve simply called it mover. The great thing about that is that when we create a script file that can move any kind of object, by any speed, in any direction. We can simply drag and drop that onto any particular object that we want to move it in any direction that we want to. Now before we go ahead and drag and drop the script file here onto this car, I’m going to select the car object inside the scene. Now I want this car object here to move in a particular direction. So in this case, I want the car to move here along what is effectively the Z direction.

So to do this, or at least in this case, it’s the Z direction. But notice that if I rotate the car, for example, to face in this direction, I would no longer want to keep moving the car along the Z direction. In fact, I would want to keep moving the car along the direction it was facing. Now to control this we’re going to use the forward vector. When I select this car object in the scene, you’ll see that there’s a blue axis pointing upwards out of the roof of the car. The blue axis is the forward vector. It represents the direction in which the object is looking. In this case, this says it’s looking right upwards. So if we were to move the car along its forward vector, it would in fact move upwards in the scene. And I really don’t want that. Instead, I want the car to move in the direction that it’s looking. I can easily reconfigure the axis of this object by using empty game objects and parenting. Let me show you how.

I’m going to move up here to the File menu and choose Game Object, Create Empty. And I’ll simply name this object car root. I’m going to locate the position at somewhere along here, hopefully in about the center of the car, so somewhere about there like that. Then, I’m going to drag and drop the car object underneath car root, so that it becomes a child of that object. Now when I select the car object, you’ll see that the blue forward vector is in fact pointing forwards along the same direction in which the car is looking, and that is exactly where I want it. What I can do then is simply drag and drop the mover script onto the car root object, not the car object, so that we can move the car along its forward vector. I’m going to double-click on the mover script to open that inside monodevelop here or rather inside visual studio so that we have our function here, or our class, to create a new mover script. Now inside his script file, the first thing that I want to get access to is the transform component of the car object. That is the component, the transform component attached to the car because this encodes the position variable. And it’s the position of the object that we want to change over time to create movement. So I’ll go back to visual studio here and create a new private variable that’s going to reference the transform component. It’s initially going to begin with the value of null, but we can easily fix that because during the awake function we’re going to assign the transform component to equal the component, the transform component, attached to the object. To do that we’re going to use the Get Component function. And we’re searching for the transform component, and this is assigned to the variable This Transform.

The next thing that we want to do is control the speed at which the object is going to move along the forward vector. To do that I’m going to use a floating point variable to specify speed over time. I’ll create a new public variable, which is floating point here. And I’m simply going to call it speed. I’m going to set it to the initial value of one. That is 1 meter per second. The car is going to move forwards along its forward vector by 1 meter per second. We can create the movement functionality inside the update function which happens once on every frame. And the code to change the position looks like this. By using this transform.position, I can simply increment the position, that is it equals the position plus an additional set of values. And in particular, the new values are going to be the forward director, or the forward direction multiplied by the speed of the object. The object is going to be looking at potentially many different directions, and these are all defined by the forward direction. So I’m going to use forward here, multiplied by speed. I’m taking the forward vector and multiplying that by our speed to increment our direction.

I’m going to save this file by using Command-S or Ctrl-S and minimize it to return back to Unity. The code will go ahead and compile. If I select the car root object here at the top, I can use the speed variable here to control the speed of the object. Initially, I’m going to set that to a value of zero to indicate that the object is not moving at all. If I press Play on the tool bar, we can see that the car remains in place. If I begin to increment this by a particular speed, you can see that the car moves forward. In fact, I only need a very small value here. I’m going to set this to something like 0.01, and already you can see that the car is moving forwards by that particular speed. In addition to that, I can also rotate the car to face in a completely new direction, and the car will always travel in the direction that it’s facing because it’s moving along the forward vector.

That’s great. We’ve now created movement for this car along the forward vector, but there’s still more to do. In this video, we saw how to move an object along it’s forward vector. We created a script file that can transform the position of an object along the forward vector by a particular speed. However, there’s a lot more other things that we might want to add to an object to make it move more effectively.

[Audio Ends] [0:08:13]