Moving objects in Unity

Daniel Kirwan
5 min readMar 24, 2021

--

Moving a player around a game environment is the most fundamental part of any game and it’s normally the first part that is taught, and in Unity, it can be very simple.

First off we need to figure out how to move a Unity game object when the game is in play mode. We can do this by creating a script and attaching it to a game object in the Unity editor. I have created a 3D cube and a C# script, both are called Player.

To move a game object in Unity you need to manipulate the values in what is called the Transform component. Every object created in Unity will normally have one and it can be found right at the top of the list in the inspector window.

Transform component

The transform component controls the position of the object in the game world. To move the player we will be using a special method called Translate which works on the transform component. https://docs.unity3d.com/ScriptReference/Transform.Translate.html

transform.Translate(Vector3.right);

Above we’re moving the game object to the right on the x-axis by 1 every time this method is called, and this method just happens to be in Unity’s Update method in my Player.cs file. The Update method is called every frame.

Below is a gif of our game object moving a what seems like light speed due to our above code.

We would ideally like the player to move at around 1 metre per second. So, what we can add to that line is a small multiplication.

Time.deltaTime

This converts from frame rate dependent to real-time seconds, minutes, and hours. It means the time it took to complete the last frame to the next frame.

This means we move a lot slower.

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

This will move the player at a steady pace.

To make the player move at an even better pace we can adjust the formula more by adding a variable.

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

Now we moving at 5 metres per second. This feels like a much better pace for the auto-moving player to move at.

We can make this even better by creating a variable that is specific to our speed.

[SerializeField] private float _speed;

I have added in a private float variable called _speed. Now, you may be thinking, but what does the[SerializeField] part mean? Well, let me tell you. It means that the variable stays private so that other scripts cannot access it but we are able to adjust the variable in the Unity editor. This is normally done so that if you’re working in a team and you have created a new system, you can allow the level designers to adjust the variables they need to make the level feel good.

Moving the Player game object with keypresses using Unity’s input manager

Unity has a default state for the Input manager that allows us to start moving a player straight away. You can find the Input manager by going to edit ->project settings -> input manager

Here you can see that there are already a plethora of inputs already setup but the ones we’re interested in right now, are the Horizontal and Vertical ones. You can change which keys the input manager will be trying to detect here but I’m going to leave it as the standard set up WASD.

Now we need to get this working in the code. The Horizontal and Vertical axis both return floats between -1 & 1. This means that when we press any of the WASD keys that a float is returned and we can use that float to move. But first, we need to store that float somewhere and the best place for this is in a local variable for the horizontal movement.

I have created a private float _horizontalMovement and this will store the horizontal float that is returned to us. Now, we need to use that variable to move our player. To store the Axis float we use the following:

_horizontalMovement = Input.GetAxis("Horizontal");

This stores the Horizontal axis float into _horizontalMovement so that we can use it somewhere else. Like now, when we want to move the player. Our code that moved the player automatically to the right is now changed slightly to:

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

Above you can see that the player is moving from right to left and back again. I am doing this by pressing the A key to move left and the D key to move right. That’s it, it is that simple for very easy movement in Unity. We can now do the same for the Vertical axis.

private float _verticalMovement;

Now above or below the previous player movement line we add another similar line but with a couple of changes.

_verticalMovement = Input.GetAxis("Vertical");transform.Translate(new Vector3(_horizontalMovement,_verticalMovement,0) *_speed *Time.deltaTime);

And this is it for this article on player movement in Unity. Check back for more articles coming soon.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet