Unity Beginner to Hero Part 2: Player Movement

Hello. Last time, we created a new project in Unity and got a Capsule to move around using a simple script. This time, we’re going to add player input to drive the Capsule’s movement.

Open the project up from the previous lesson, and open the BasicMovement script. This time around, we’re going to add some variables in to the class. Just below the BasicMovement class declaration, add in a single variable.

public float speed = 10.0f;

Now, modify the line we added last lesson to instead look like this.

transform.position += Vector3.right * speed * Time.deltaTime;

Go back to the editor and push play. You’ll see that your Capsule moves much faster now; it moves at the speed defined by your variable in the class (in this case 10). This is a bit fast, so let’s slow it down. Select the Capsule. In the Inspector, you’ll be able to see your attached script, as well as the variable speed.

Because that variable was given the access modifier public, it now appears in the inspector, and can be changed without having to recompile your code.

Back in our script, let’s take a closer look at what our movement line is doing, left to right. In the last lesson I noted that every game object in Unity has a transform component.

transform.position += Vector3.right * speed * Time.deltaTime;

Here, we are accessing a Transform component; in this case the transform of the game object that this script (BasicMovement) is attached to. Once we have that transform, we can modify it’s values, namely position, rotation and scale. In this line of code we get the position of the transform, and then we use the += operator to add a value to it.

So what exactly are we adding? A Vector3 is a 3-dimensional vector that Unity uses to (among other things) to store the position of an object, as x, y, and z. To translate an object’s position, you add one vector to another.

Our controller moving along the the X axis, shown as the red arrow. The path he travels is shown in a red line behind him.

Our controller moving along the the X axis, shown as the red arrow. The path he travels is shown in a red line behind him.

transform.position += Vector3.right * speed * Time.deltaTime;

Vector3.right is a constant value, shorthand for writing Vector3(1, 0, 0). In other words, it is a vector pointing exactly along the x axis. If you go back into the editor and look at the Scene panel, in the top right you’ll see a little axis gizmo showing the three directions each axis points in.

If we just added Vector3.right to our position each frame, we’d move exactly 1 unit to the right every frame. However, we want to be able to control how fast our Capsule moves, so we multiple (scale) the Vector3 by speed. Multiplying a Vector3 by a single scalar value (just one number, like 10 or 25.5) multiplies each individual component by that value. e.g., Vector3(2, 3, 0) * 10 = (20, 30, 0).


transform.position += Vector3.right * speed * Time.deltaTime;

Finally, we multiply the vector again by Time.deltaTime. Because different computers will run your game at different framerates (usually from 30-60 frames per second), we need to the game simulation to be framerate independent. This way, your Capsule always moves exactly speed units per second to the right, regardless of whether 10 frames or 100 are rendered per second. We’ll talk more about this in later lessons.

Now that we are experts in Vector3s, let’s build one of our own. At the top of the Update method, add in the following line:

Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));

Here we have declared a new variable of type Vector3 and named it “input”. We set it’s x and z values to be the result of the Input.GetAxisRaw methods for the axes Horizontal and Vertical. Input.GetAxisRaw is a method that returns either a 0, 1 or -1 depending on which direction an input axis is pointing. We’ll talk more about how to create your own input settings in future lessons, but for now it’s enough to know those axes are set by pushing down WASD (or the arrow keys). Finally, modify the translation line to be:

transform.position += input * speed * Time.deltaTime;

Save and play, and feel the freedom of two axes of movement!

Lesson 2 Complete Project

Thanks for reading! If you have any problems with the above tutorial, general comments or suggestions, please post below. It’s always appreciated!

5 thoughts on “Unity Beginner to Hero Part 2: Player Movement

  1. This tutorial series is a godsend! Video tutorials seems to be an increasing trend, but I need written tutorials so that I can go at my own pace and absorb them. Thank you so much for taking the time to make these!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s