Player Movement in Unity

Larry Tweed
4 min readJul 23, 2021

At a very basic level, movement is an essential element of most games. There are a few steps you can take to get the hang on movement (player or otherwise).

Before an object moves, you’ll need to know in which direction it is headed. You’ll need to get some input from the (human) player. In the image below, the game player is simply a cube.

The movement of the player object will be handled by a script called “Player”. This script will be attached to the player object. This is a common practice… keeping the objects functionality within control of that object and/or its scripts.

We will use very simple built-in functionality to gather the movement direction from the user. We will move only in the X and Y directions (we don’t need to move into or out from the screen… the Z direction).

These commands are provided by Unity to gather keyboard, joystick, and gamepad input. The Vertical axis is the Y direction (up, down) and the Horizontal axis is the X direction (left, right). Each of these will return a float number between -1 and 1. In the case of keyboard control, -1 will mean left or down, and 1 will mean right or up.

You can look under “Edit->Project Settings->Input Manager” for more mappings and options. So we have some basic information, but if we put this in a script and ran it, the player object would fly off the screen before you could blink. We need to control the speed as well.

It’s as simple as that. The number I have initialized the speed to was discovered by testing.

Once we have all the info needed, we use Unity’s “Translate” command. Translate means to move, as opposed to rotating or resizing. Translate takes a Vector3 as an argument. A Vector3 simply holds 3 values for (X, Y, Z). These may or may not represent locations, but can be used to store many values that are unrelated to movement.

In our case, we initialize the Vector3 (called direction) with the input values we’ve already got. Remember, we are not using the Z value, so it is set to 0.

Here is the code to actually move the object:

First, we have the Vector3 direction. When it is multiplied by _speed, each member of direction is multiplied by that number. For example, if direction=(1, 1, 0), multiplying by _speed (3.5) would result in (1*3.5, 1*3.5, 0*3.5) = (3.5, 3.5, 0).

You’ll notice the addition of “Time.deltaTime”. We need to account for varying frame rates and make our code frame rate independent. We don’t want the object to speed up on a faster machine or if your computer is just having a good day! We want it to perform consistently across machines. The way we handle this is to multiply by “Time.deltaTime”, which is a very small value that holds the time the previous frame took. This smooths out variations that might occur between faster/slower machines. Since this is a very small number, it turns our direction values into very small numbers also. When you multiply by your frame rate, say 60 frames per second, this gives us something akin to “real time

And that’s it! You can now control your player. Here is the complete Player script. Note, it must be attached to the Player object (the cube).

And here’s what that small amount of effort can do.

--

--