Unity Dev: Player Movement

Alex Somerville
4 min readMar 18, 2021

Now that we got our Player object looking like the coolest cube on the block in Unity Dev: Player Prototype Cube, let’s give it some life.

Add a new folder to your “Assets” directory in your Project window called “Scripts”. This will be the new home for all of your C# scripts related to this project.

Inside of this folder, Right-Click > Create > C# Script and name your new file “Player”.

IMPORTANT: Rename the file immediately to “Player” before clicking away, or you’ll need to make some edits inside of the file itself.

Drag the new C# script on top of the “Player” object in the Hierarchy window to attach the script to it.

Double-clicking the new file should launch your preferred IDE. If not, Edit > Preferences > External Tools and choose your preferred IDE from the “External Script Editor” dropdown.

Variable Basics

Variables are very common in programming. You can assign a value to a made up name and then use that name in place of the value you want in multiple places. You then only have to change the value in a single place in your code.

[SerializeField] private float _speed = 5.0f;

As an example, let’s place the above line immediately inside of the public class Player : MonoBehaviour definition.

[SerializeField]
This allows your Unity editor to read the value of this variable and display it as an editable field within the editor, allowing others working on the project to tweak settings, such as the movement speed of the Player object.

private
This denotes that the “_speed” variable may only be used or changed by the Player script itself. No other script can directly modify this value. The underscore in front of the name is a best practice that helps to denote that a variable is private when used throughout the code.

float
Variables in C# are typed, meaning you must declare the data type that the variable will contain.

Player Input

Taking a look at Edit > Project Settings > Input Manager, we can find that Unity is already helping us do a lot of heavy lifting by coming installed with this Input Manager. Most importantly for right now, is our Horizontal and Vertical axes, which we can reference to get our cube moving around.

Getting values from these in our code is pretty simple. Since we’ll want to maintain movement throughout the game, we’ll want to be tracking our input by way of the Update function which is conveniently auto-generated into the blank C# file we just made.

void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
}

In the above code, we’re defining two variables of type float and telling them that they are equal to the return of the method GetAxis which we pass the argument “Horizontal” and “Vertical” in two separate statements. These are the names of the objects as shown in the Input Manager we just saw.

This won’t make anything happen on its own though. If we were to Debug.Log(horizontalInput) and press the A or D keys on our keyboard, we could see numbers anywhere from -1 to 1 logging out, depending on which key is pressed. This is the user input we need! We’re going to use this value to help us tell the our Player to move around the screen.

void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");

transform.Translate(Vector3.right * (horizontalInput * _speed * Time.deltaTime));
transform.Translate(Vector3.up * (verticalInput * _speed * Time.deltaTime));
}

transform

In the above code, we’ve added two more lines. Let’s break one of them down for easy understanding.

transform.Translate
This is a Unity method that tells the object’s transform to according to the values provided. You can read more about it here.

Vector3.left
This is shorthand for Vector3(1,0,0) . The numbers in here refer to the x, y, and z axes. We want to use the positive side of the axis in both cases, right for horizontal movement, up for vertical movement.

Vector3.right * (horizontalInput * _speed * Time.deltaTime)
Here we’re multiplying our Vector (direction) by a few things. Our horizontalInput is a float between -1 and 1, depending on the key pressed. _speed is a variable we set as a float with the value 5.0f. Time.deltaTime helps us normalize the movement speed of the object so that we’re not flying off the screen.

Note: Parentheses are used around horizontalInput, _speed, and Time.deltaTime to multiply those items together before applying them all to the Vector for some minor optimization.

You’re moving!

If you save your file and run your game in Unity, you’ll now find that you can move your cube around using the WASD or the arrow keys.

IT’S ALIVE!

--

--

Alex Somerville

On a quest to become a game developer. Still sometimes providing unsolicited advice about how to function in society.