Making User Inputs To Move A Player

Kenny Pruitt
Unity Coder Corner
Published in
4 min readFeb 20, 2023

Objective: Move A Player Using User Inputs

To move a player, first you must use the Input Manager to understand how to access it so you know the names of the controls you are trying to use. To do this, in Unity go to Edit — Project Settings — Input Manager.

Then you can go to your Player script and access any of the inputs in the Axes by getting the float value of the Axes. To do so, you can make a float variable that gets the input from the Input Manager based on the name of the Axes. The name of the Axes is stored as a string variable.

Once you have the float variable, you can then use it as a multiplier for your basic movement. This will allow you to move horizontally.

To move vertically you basically do the same thing for the Vertical Axis and multiply it by the Vector3.up.

Now if you go back to Unity and press Play, you will be able to move vertically and horizontally using the keys listed as your Negative, Positive, Alt Negative, or Alt Positive. I also increase the Player speed in the player script to 20, just to make it a little faster.

It is a good idea to limit the lines of code being run in an update function as much as possible. The more lines being read every frame the more it takes from performance over time. So it is a good idea to optimize code when possible. The way I usually go about doing this is to, make your function work the easiest way possible and test to make sure it works. Then look at your code and see how you can simplify it, or if you could move it to something other than an Update function.

For example, the movement code up above could be simplified to just getting a new Vector3 of the movement direction then removing the 2 lines for getting the float values, and combining them into a single Vector3. Finally you would just replace Vector3.right and remove the old float, and multiply that by the playerSpeed and time.deltaTime.

To better organize your scripts, and make it easier to come back to scripts for future editing, it can be a good idea to separate scripts into what their purpose is. For example here, if we were planning to add more inputs, it may be a good idea to make an InputController script that handles all your player inputs in one place.

If you did this though you also need to make sure to get the player information that you need. As you can see in this picture I created an InputController script and moved my movement direction into it, but the playerSpeed is coming up with an error.

This is because that reference to the float does not exist in this script. So in order to get it, if your InputController and Player scripts are on the same gameobject you can simply get the Player component from that gameobject and use any public variables it has.

Now that you have a reference to the Player script, you can use any public variables or functions in it. With that, you can now fix the error of not being able to find the playerSpeed by first referencing your Player script, then using its variable.

Never forget to test often, it’s easier to find issues if you are testing often to make sure something you just made or changed is working properly.

--

--

Kenny Pruitt
Unity Coder Corner

Unity Game Developer, C# Developer, and Software Engineer