Simple Player Movement in Unity
This article is going to go over the basics of getting player input to get the character to move in 2D. To get started you need to have the player character created and have added a C# script to it.
To handle player movement you need to understand what controls the position of the player, the “Transform” component. The “Transform” controls where the object is in the scene, Position, what direction it is facing, Rotation, and how big it is, Scale. If you want to learn more about Transform you can click on the question mark in the top right of the Transform box. This will open up the Unity documentation with all of the information about the Transform
Now open the script for the player character. Inside the script in the Update function is where you will control the player movement. To do this we need to access the Transform. In the script we do this using “transform” with a lowercase t. Then to get the object to change positions we use the translate function, like transform.Translate(). Now you need to put a Vector3 in the function. This will tell the translate function how much to move the object in the given direction. If you give it Vector3.right the object will move to the right. If you give it Vector3.up it will move up. You could also give it a new Vector3 like this new Vector3(1, 1, 0) that would move it up and to the right. Here is an example using Vector3.right and the resulting movement.
In its current state it moves pretty fast to the right. This is because Update is called once per a frame and Vector3.right or Vector3(1, 0, 0) is causing it to move 1 meter to the right each time it is updated. To slow it down you can multiply the Vector3 by Time.deltaTime. That is equivalent to the amount of time between each frame. This will cause the object to move at 1 meter per second. Here is the new result.
Now to allow us to set the speed of the movement you need to introduce a variable where you can set how fast you want the object to move. You will then multiply the Vector3 by your new variable to set the movement speed of your object.
Now you are ready to include the player input. First you need to check what keys are setup for player input. To find this in the Unity editor, in the menu, click “Edit” then “Project Settings”. In the “Project Settings” window select “Input Manager”, then click “Axes”, and then “Horizontal”. Here you can see what is set for input. It should be set for regular input of the left and right keys and the alt of a and d.
In order to get the input you will need to use Input.getAxis(). This will allow you to enter the axis you want the input from. So far we have been using Vector3.right so we will start with the horizontal axis. If you assign this to a variable then it will be easy to use in the translate function. Doing this will allow you to start controlling the horizontal movement of the object.
To add vertical movement you just need to duplicate what you did for the horizontal movement, but make two changes. First, you need to change the Vector3.right to Vector3.up, this will make the movement vertical. Next, you need to change the input axis from horizontal to vertical, this will take input from the up and down arrows or the w and s keys.
That’s it, you did it! Your character is now moving in 2D based on player input.
Good Luck Adventurere!
-Chris