I like to Move It, Move It

Player Movement

Hey all you kids… so I was looking back through our Space Shooter game there were a lot of mechanics I missed talking about. Mainly the Player movement and what variables are. Got the enemy working but wanted to firm up a bit about the Player mechanics before we dive into another game object or two.

In the next couple of articles, we’re going to go back a bit and do a couple of quick breakdowns. This one, let’s talk about Player movement.

What is Player or GameObject movement? Or more, how do we make any GameObject move? If you look at the 3D cube we named “Player”, you will notice once selected, in the Inspector of Unity, it has many components listed. At the top, we see something called Transform.

GameObject Transform

Transform is a very important component as you will have direct access via code to alter it for Player movement. You will see here that in 3D space, there are 3 axis like in Translate; y-axis: up/down, x-axis: left/right, and z-axis: in/out. You also have Rotate x, y, z and Scale x, y, z.

Arbitrarily, our cube/Player is…
Translate is x: -2, y: -3, and z: 0.
Rotate are all 0 meaning no rotation in any axis.
Scale are all 1's meaning 1 to 1 ratio in all directions as well.

How we alter this in code is usually with a property called a Vector 3 (x, y, z).

Let’s make a script and add it to our Player GameObject to see how it works.
Create a new folder called “Scripts” and right mouse button (RMB) click the folder and select Create>C# Script. The text will highlight blue so name it “Player.”

Assign new Player script to cube

Assign the new Player script to the cube named Player and then let’s double click the new script to open it up in Visual Studio.

We said the cube was at Translate -2, -3, 0 right? Well let’s say we wanted to set the position to 0, 0, 0 or the origin of the 3D world, how would we do that? Based on Unity’s documents, we would write this code at the start of the game under the Start(); function or code that takes place once at the beginning of the game:

position = 0, 0, 0

This snaps the cube into place at 0, 0, 0, when we play the game. Even though it was originally -2, -3, 0, the code forces the new position. Nice!

Now let’s see how we can use transform to Translate the object over time. Where would we place the code here? We would add it to the Update(); function as this would call the code here every game loop. Usually (we hope) about 60 meters per second. Let’s add a similar code here but this time use “Translate” instead of “position” with 1, 0, 0 as it appears here for a change in the x-axis of +1:

Translate = 1, 0, 0 every frame

Save and play the game. What happens? We move to the right of the screen like super fast correct? Maybe about 60 meters per second. Now try, -1 in x-axis, save, and play. We move to the left. You could also do a more short hand approach like:

Vector Right

You can find all these short hand methods here under Unity’s Vector3 Properties.

This is all good but how do we control how long/fast the cube moves? Well, we do that by assigning the “meters per frame” to “meters per second.” To do that, we multiply by Time.deltaTime:

Move to the right 1 meter per second

Now we are moving 1 meter per 1 second. Cool! As you may have guessed, we can also multiply by a number to increase the speed. For example, multiply by 5 to make it move 5x as fast to the right.

Move to the right 1 meter per 5 seconds

That about does it for today. Let’s talk next time how to assign the number 5 to what is called a variable that use used in several places in a script. See you next time!

Passionate animator and game developer; Wesley is a co-founder for Multivarious Games and a Creative Director in elearning at Xcelerate Media.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store