Day 4: Simple Player Movement In Unity

In Unity, we need to use scripting in order to manipulate player movement.

C# scripts that we create in Unity will inherit from MonoBehaviour by default. Start and Update functions are also provided by default when we create a script. For further information, you can simply check here.

Just a quick reminder, we should not forget that Start method will be called only once at the beginning, while Update method will be called every frame. Since making the player move requires continuous input, we need to code it under Update method.

Every game object that exist on our scene view has a Transform where we can observe and manipulate objects’ position, rotation and scale. So, why can’t we make the player move by changing it’s position values according to our input? To do this, we first need to reach to the transform. You can always find detailed information on Unity Documentation by simply clicking the “?” on the right side of Transform field (or any field you want to). If we visit Unity documentation regarding transform (click here), we can easily observe Translate method may help us:

Thus, we are going to use transform.Translate command to make our player move according to our input. By typing a dot after transform, we can easily read or change the corresponding values of a game object’s transform easily.

For more detail regarding transform.translate, you can visit here. Unity represents all 3D positions with Vector3 and 2D positions with Vector2. Since we are working on a 3D game, we are going to use Vector3 in this case.

To give an example, if we want to move the player upwards, we type the following in the Update function:

Transform.Translate(new Vector3(0,1,0));

However, you will observe that our object will instantly move to upwards. Since Update function runs a standard speed of 60 frames per second, this indicates that player moved to upwards at a speed of 60 meters/second.

Unity is frame rate dependent by default. It basically means that Unity assumes that every computer will be run the game/application we are designing at a rate of 60 frames per second. Certainly, this is not the real case.

To solve this problem, we need to make Unity time dependent rather than being frame dependent. That’s where “time.deltaTime” gets into play. ‘time.deltaTime’ command simply calculates the time passed for the computer render a new frame (time between 2 frames). By multiplying the movement by time.deltaTime, we will gather a smoother movement.

For instance, if we want to move the right:

transform.Translate(Vector3.right * Time.deltaTime);

To speed up the movement, we can simply multiply with a speed variable. Since we want to make the speed variable only changeable from the corresponding script, make sure that it’s a private variable with a value assigned different than 0.

transform.Translate(Vector3.right * _speed * Time.deltaTime)

Gathering User Input

It is possible to gather information regarding Input Manager of Unity by simply clicking Edit — Project Settings — Input Manager.

As we can see on the Input Manager, we can gather the input commands according to axis. To gather the data for horizontal axis, we simply type :

Input.GetAxis(“Horizontal”)
Input.GetAxis(“Vertical”)

As you can see here, the idea is pretty straightforward. In Unity, we always type strings within brackets.

Input.GetAxis will always return a float value in between -1 and 1. When we do not press any movement button, both vertical and horizontal input will return the value 0. For instance, if we hold the D or right arrow button, horizontal input will return the value -1.

In the way of clear coding, it will be better for us to createa variables for these axis we want to gather. Since horizontal axis will return a value onthe X axis and vertical axis will return a value on the Y axis, we can directly use the input we gather in our transform.Translate method.

By typing the following into the Update method (of course you better create a void Movement method in order to keep the Update more clear), we can easily make the player move.

Lastly, I would like to remind you again not to forget to assign a value to the _speed variable as Unity will gather it as 0 by default if you forget to do so.

Unity Developer