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.
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
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.
In the update section of the script, we will add our code for moving the player.
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
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.
To use these axes from the input manager, we will need to create a variable to store the information.
Next, we will add the variables to our transform. Translate statement.
Now you should be able to get things moving in your game.