Creating Simple Player Movement

The goal is to make a simple player movement script for a 2.5D game to get the player moving around.

First things first, open up a fresh New Scene in Unity.

Opening new Unity Scene

Next, create a Cube that will be used as the Player. There are two ways to do this.

2 Ways of Creating a Cube

Now rename the Cube to “Player”.

Renaming the Cube

If the scene has not been saved, save the scene. Make sure to save it in the Scenes folder to stay organized. Remember to save early and save often!

Saving the Scene

Now that everything done so far has been saved, it is time to create the script to control the Player gameobject. Before creating the script, it is best to stay organized and make a Folder for scripts. Create a folder and name it “Scripts”.

Creating a Scripts Folder

Time to create the script for the Player and name it “Player”. When naming a script, the script’s filename and the class name inside the script must be the same or causes an error. With the Player script selected, the Inspector window will show a preview of the script, and the class name is the same as what the file was named.

Creating the Player Script

Now drag the Player script onto the Player gameobject and then open the Player script to begin programming the code to move the Player.

Adding the Script to the Player

With the Player script opened, notice that the name of the class, which is Player is the same as what the script was named when created. If it is not, make sure to change it so it is, or else there will be an error.

View of the base Script

Notice that after public class Player, there is : MonoBehaviour.

MonoBehaviour is a base class that every Unity script is derived from. This is known as inheriting and what that means is the Player class will have access to the MonoBehaviour class’s functions like Void Start and Void Update, but most importantly, it allows the script to be attached to a gameobject like the Player.

Showing Void Start and Update

To make sure that the Player can always be found easily at the start of Play Mode, add a line of code in void Start. A transform is what holds the information of a gameobject, such as position, rotation, and scale. The information is stored in a Vector3 which is a set of 3 floats representing (x, y, z). So by changing the transform’s (the Player’s) position to equal a new Vector3 with the values of (0, 0, 0) at the start of Play Mode, it will move the Player to (0, 0, 0), which is the center of the screen. After the code has been entered, make sure to save the script (Ctrl +S), and make sure to save it every time you make a change, or else Unity will not know it has changed.

Now back in Unity, move the Player away from the center and enter Play Mode to see the script work. Now that the Player will always be in view at the start of Play Mode, let’s code the user input.

Cube Moving to 0,0,0 at the Start

In void Update, add in a local float variable that will be used to hold the input from the Horizontal axis defined in the Input Manager.

Code to Capture the Horizontal Input
Input Manager showing Horizontal Axis

Now to apply the input to the Cube. What transform.Translate does basically adds the Vector3 that is passed in the parentheses to the transform.position of the Player moving it each time Update runs.

Code for Moving the Cube

The code is adding or subtracting 1 unit (meter) from the x of the Player every frame, so about 60+ meters a second.

Cube Moving at Light Speed

Time.deltaTime is the amount of time in seconds since the last frame. By multiplying the Vector3 by Time.deltaTime, it lowers the amount the Player’s position is changed by.

Adding Time.deltaTime

It is moving too slow now, but it can be easily adjusted by adding a variable.

Cube Moving Super Slow

At the top of the class, create a new public float variable named “speed” and have it equal to 5. Now in the Translate function, put the Vector3 multiplied by speed multiplied by Time.deltaTime.

Adding the Speed Variable

Now the Player is moving at a fairly nice speed, about five units (meters) a second.

Cube Moving at a Reasonable Speed

If the speed needs to be adjusted, it can be done so right from the Inspector since the variable is public.

Speed Variable in the Inspector

To implement vertical movement, follow the same process as before but get the Vertical axis and put verticalInput in the y of the Vector3.

Adding the Vertical Input

Now the Player can move both vertically and horizontally.

Cube Moving Vertical and Horizontal

To clean up the code a bit, create a local Vector3 named “direction” and have it be the new Vector3 with the horizontalInput and verticalInput. Then replace the Vector3 in the Translate function with direction.

Combining to make Direction

With the Player moving vertically and horizontally, the simple player movement script is complete.

Unity Developer, Software Engineer, Game Designer

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