Movement using User Input in Unity
How to move the Player with keys
This article will cover how to do basic movement of a game object in Unity using user input.
To test you can try the same setup, or just use your own project. Here are the steps to create the same or similar setup. We start off with an empty Unity project.
1. Create a capsule to use as the Player
Other objects are find. This will be the game object we are going to move. It’s renamed to ‘Player’ and colored blue, but you can skip that.
2. Create a ‘Player’ script
In the project panel under assets. It’s recommended to make a habit to create folders for the different types of assets for the overview. In this case we created a folder name ‘Scripts’.
3. Attach ‘Player’-script to ‘Player’-game object
Either drag the script from the project panel to the ‘Player’-game object or add it through the inspector panel from the ‘Player’-game object.
How to program?
Often people think of programming as a special unique gift. The reality is mostly being able to know how to use google. From there you just experiment and try to get things working. There are different degrees of proficiency in figuring out how to do things, but essentially it’s how to use google and test stuff.
Set start position of player
We will search how to change player position first then add user input. This will break our challenge down in two parts, which makes it easier to solve. After using google we found the following line of code to set the start position of the player.
transform.position = new Vector3(0 , -3, 0);
To break it down:
transform — tells us we want to access the transform-component of the game object our script is attached to
.position — the dot position tells us we want to access the position part of the transform
= — in programming the equals sign is often better worded as “becomes”.
new Vector3(0, -3, 0) — the is new value in the format Vectoor3. This means it has 3 parts. x, y and z.
; — end of line
In human language this line of code would translate to:
The position part of transform becomes a new value in format vector3 with values 0 for x, -3 for y and 0 for z.
Personally I find being able to ‘translate’ my own written code to human language help me a lot to understand it. Therefore I try to write my code in such a way it’s easy to read.
Unity is component based
The Unity Editor workflow is called component based. We created an cube component, we created a script component and we linked the script to the cube. Consider each as a separate part just like a part of your computer. Each part has their function and does their own thing. If you link them up in a certain way you can get a working computer. We won’t go into details for now. Just remember there are lots of parts and they can be linked together is various ways and that is the Unity way of creating games. Now if we select the player and look at the Inspector panel we see a whole bunch of components. Each component has all kinds of attributes. In our case the transform component is the one we will be working with to make it move.
In the image below we move around the player and see the values in the transform component change. This is a way to figure out what value we want to manipulate with our script.
Move the player
Let’s put our main programmer skill to use and go to google.com and search for something like:
“How to make a player move in unity”
Remember: In programming there are multiple ways to solve things. Some may be more optimized than others, but what counts in the end is if it works for your situation or not.
In this article we choose to use the pre-mapped controls of Unity to get user input and the transform.translate function to move the player around.
Let’s start with the input. We will be using Input.GetAxis(“name”). These use pre-mapped controls of Unity. You can find these control in Unity under:
Edit > Project settings > Input > Axes
Remember the names of “Horizontal” and “Vertical”.
Tip: when you google and find a function and you’re not sure what it gives as output use Debug.Log(foundFunction);
This way you can experiment and see how things work. For example you can test the output of Input.GetAxis(“Vertical”) with:
You can see in the console how the values change based on your input.
Notice the numbers change between -1, 0 and 1 at the bottom. This means these values are sent to our script when we press the keys accordingly. Down changes it to minus one, up changes the value to plus one and when we don’t press anything it becomes zero.
When you use Unity you’ll often use the official documentation and look at the examples. This function is put in the Update() which is executed every frame. So every frame we send a Vector3 position how our player should be moved based on the position in previous frame.
Suggest a change Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all…
We’ve seen the Vector3 type value just like how we set our player starting position. By combining our User Input into a vector3 we get:
transform.Translate(Input.GetAxis(“Horizontal”), Input.GetAxis(“Vertical”), 0);
The x-part of the Vector3 is our horizontal input. So if we press the left-key our x-value will be changed to minus one. If we press the right -key our x-value will be changed to plus one. We did the same with the vertical axis for going down and up. The last value is just zero, because we limit our self to moving in 2D for now.
Make move speed independent of frame rate with Time.deltaTime
In the transform.Translate example we see “* Time.deltaTime”. If we click on it we jump to the documentation of it.
Since Update is executed every frame Unity renders it could be many times a second or very few times a second. This could depend on the machine you’re running the game on. A very fast machine with 120 frames rendered per second would say move to player by one, 120 times in a second. The same game on a slow machine with 12 frames per second would say move the player by one, 12 times in a second. This will result in the game to behave very differently depending on the machine.
To correct this we use “* Time.deltaTime”. This is the time is took between this frame and the previous frame to render. If it took a lot of time we get a big number so the player moves more and if we got a high fps the number will be small and only move a bit in one frame.
With this added our code should look like:
transform.Translate(Input.GetAxis(“Horizontal”) * Time.deltaTime, Input.GetAxis(“Vertical”) * Time.deltaTime, 0);
Adjust the speed we move at
Now we can move our player at the same speed on different machines. To adjust the speed we move we put in another multiplier. We make it available in the Inspector by adding [Serializefield] in front.
public class Player : MonoBehaviour
[SerializeField] private float _speed = 5f;
// Start is called before the first frame update
//set starting position
transform.position = new Vector3(0, -3, 0);
// Update is called once per frame
transform.Translate(Input.GetAxis(“Horizontal”) * _speed * Time.deltaTime, Input.GetAxis(“Vertical”) * _speed * Time.deltaTime, 0);