Unity 2D Movement with ‘transform.Translate’

When making a 2D or 3D game, being able to move around a player in game space is a must in many cases. Today, I will explain player movement in 2D space.

The key is using a method called transform.Translate.

So, what is transform.Translate? According to Unity API, transform.Translateis “Moving the transform in the direction and distance of translation.”

Horizontal Movement & Vertical Movement

Let’s first look at the simplest form of movement —’ Horizontal & Vertical Movements’ with a cube.

Horizontal Movement

When an object (a cube in this case) moves left or right, you will notice that x value under ‘Transform’ in inspector changes.

  • X value changes + to right direction & — to left direction.

Vertical Movement

Similar to the horizontal movement, for the vertical movement, Y value under ‘Transform’ in inspector changes when moving the cube up or down.

  • Y value changes + to up direction & — to down direction.

Based on this, we can understand that in order to move an object, the value in transform position must be changed — X value for left or right & Y value for up or down.

So, how do we do change the transform position? That’s when the function transform.Translate comes in handy.

Transform.Translate

As I described earlier, according to Unity API, transform.Translateis “Moving the transform in the direction and distance of translation.”

Ok, then how do we move an object to right for example? Under ‘void Update()’, we can write transform.Translate(Vector3.right);to move an object to the right. Let’s see what it does.

What was that?! The cube is ridiculously moving fast to the right! That’s because transform.Translate(Vector3.right); is same as ‘move the cube to right by 1 unit (same as 1 meter in real world) per frame. Now, Unity’s ‘void Update’ speed is about 60 frames per second in average (varies per computer spec and how heavy the project file is). This means the cube is moving 1 meter per 1/60 second = 60 meters per second. No wonder it’s moving so fast! So then, what can we do to slow this object down? We can use ‘Time.deltaTime’

Time.deltaTime

The function Time.deltaTime converts the speed into a real time. Let’s take a look what happens if we multiply by Time.deltaTime into the previous code.

It is moving much slower, because we are asking the cube to move to right by 1 meter per second in lieu of 60 meters per second. What if we want to manipulate the speed? We can add a speed variable such as private float _speed = 5f;.

Multiplying speed (value 5) into the code makes the cube to move 5 times faster than previous.

But, currently, we do not have any control over this cube’s movement. So, how do we control it?

Input Manager System

One of the benefits of using Unity is that there is a preset of Input System that allows to map out user input to a character. For our case, we can use WASD keys to control the player’s movement.

In Unity, go to Edit → Project Settings, there is a tab called, ‘Input Manager’.

Under Axes, let’s take a look at the first two — Horizontal and Vertical. This is what we are going implement into our code.

You will notice that these two inputs ‘Horizontal’ and ‘Vertical’ can allow us to move up, down, right, and left with WASD or arrow keys.

  • When a key is pressed (such as ‘right’ key), the Horizontal Input will give us +1 value. Oppositely, ‘left’ key will give us -1 Value.

Writing a code to use this user input system is surprisingly similar to the hierchy of Input Manager above.

  • Input Manager → Axes → Horizontal

In code, we will write as Input.GetAxis("Horizontal").

To implement into our existing code, we will write as below.

First, we will store this function Input.GetAxis("Horizontal") into a variable called, horizontalInput.

Then, we will insert * horizontalInput within the previous line.

We can also implement the vertical movement in a similar way.

Let’s test this out in Unity!

Cube moves with WASD Keys.

Awesome! Now, we have the control of this cube!

Thank you so much for reading! More to come soon.

XR Unity Developer / Designer / Architect