Simple Player Movement in Unity

Player Movement

The most fundamental aspect of nearly all games is being able to move the player around the game scene. You may be asking yourself “how do they do that”? You're in luck because I will be showing you a basic movement using a script and then tie that to user input.

The first thing we need to do is figure out what controls the movement of the player. you will notice below when the player(cube) is moving the X and Y position in the Transform are changing.

Now that we know the movement of the player(cube) is controlled by the Transform of the game object we will need to research how to use that in a script. Unity provides an easy way to access the documentation and that is the “question mark” icon. You will find it at the top right of each component. When you click on the icon you will be taken to the documentation on that component, in this case, the Transform.

Searching the Transform Scripting API

Above you can see me searching the Transform documentation. We are looking for information on scripts so you will notice I clicked on the link for scripting. In the scripting documentation, I looked for a method that would work for what we needed, in this case, it is moving the transform. I found the method “Translate”. The syntax is below

Translate Syntax
Translate documentation

Now that we have the method we need to move our player we are ready to create the script that will execute the method and add it to the player.

Creating the Player script

In the update section of the script, we will add our code for moving the player.

Update code
Player moving fast

One thing you will notice is the player(cube) flys off the screen. After some research, you will find that you can use time. delta time to move in realtime

using Time.deltaTime
Player moving slower

Now that we have the player moving in real-time, we want to tie that to user input. First, we need to find where the user input is stored. Click on the edit menu then project settings then input manager. There you will find the horizontal and vertical axes.

Input Manager

To use these axes from the input manager, we will need to create a variable to store the information.

User inputs

Next, we will add the variables to our transform. Translate statement.

Translate using user inputs

Now you should be able to get things moving in your game.

Happy Coding!

Unity / C# Game developer