A quick start guide to player input using both Unity input methods.

Luke Duckett
8 min readMar 27, 2021

--

What are the input systems and what is the difference?

We have two methods of accessing the user input. First, there is the legacy input manager which utilizes the input manager. It is an easy system to use and get into but its downfall is the fact is not very customizable or modular.

The new input system is quite hard to get started with and some people may say it's not worth the effort. The best advantage of the new input system comes from multi-controller support and the modularity it has.

The input manager assigns a button to a function whereas the new system assigns a button to an action which then can be used to call a function. This means the key bindings can be changed easily in the new input system without the need to change all your code.

The new input system can also have different action maps. Let's say your player jumps into some water in your game. You can have a separate set of controls (action map) that you use when swimming.

Once you learn the new input system it is a powerful tool but its downfall is the difficult learning curve to get set up.

How to move an object

The position coordinates (x,y,z )are how we manipulate the position of an object in our scene

The public method “translate” is what we are going to use to move our object. When we use transform. Translate we are moving our object in the direction and distance of the translation.

If we want to move our object to the left we can use “Vector3.left”. This passes in the following vector3 (-1,0,0).

We have to tweak this code a bit though. At the moment we are calling the code at an average of 60 fps. This means in 1 second our game object moves the equivalent of 60 meters.

We can fix this using a Time.deltaTime. Time.deltaTime is a float provided by Unity that tells us the completion time in seconds since the last frame.

If we add Time.deltaTime to our translation our player no longer zooms off the screen faster than a bullet.

So now we have our player object moving quite slow, so what we can do is introduce a variable for speed and multiply our translation by it.

As you can now see with the speed variable we have the player moving at a more reasonable speed.

How to move with player input

Next, we need to capture the input from the player and move the game object accordingly. I’ll break it down below using 2 different ways of managing player input.

Input Manager (original method)

The input manager is the original method of managing player input. This way is, in my opinion, the quickest and easiest way to get player input set up in your project.

To get a list of all your input options you can go to “Edit”, click on “Project Settings” and select “Input”

We can see the horizontal axes have 4 buttons mapped to access them (left arrow, right arrow, “a” key and “d” key). Depending on the button pressed the input manager is going to pass in either a positive or negative float. As above we know that the player is also being moved left with a negative float.

Next, we need to access the input manager from our player script so we can move our game object around according to the player input.

We access the input manager by typing in “Input”. We can get the horizontal input by using the static method “GetAxis” and passing in a string of the axis we want access to.

Input.GetAxis(“Horizontal”)

If we add this to a variable we can also keep our code tidy. We can now pass this into our current translation code and now the player will move left and right when the left and right keys (left arrow, right arrow, “a” key and “d” key) are pressed.

What we might notice is that the player's input is being mapped incorrectly at the moment. When we press the keys to move left, we are moving to the right and vice versa. This is basically a math problem. When pressing the left keys, we are proving a negative number into our program, It will read as the following:

Vector3(-1 * -1 [our input]. This gives a Vector3 translation of Vector3(1,0,0). So, this issue can be easily fixed by changing our Vector3.left to Vector3.Right.

We can then reuse this logic when we get the vertical axis.

Now we have a player that can move in 4 directions.

New Input System

To use the new input system, we first have to install it from the package manager. First, it would be advised you save the project.

Now click on “Window”, “Package Manager”, change the drop-down to “Packages: Unity Registry”, find “Input System and click “Install”

Once it installs you will get a warning pop-up. This warning is basically saying that the new input system is not set up properly. Click “Yes” which will enable the backends and restart your editor.

Next, we need to add an input action to our project. “Click the “+” button in the project window and click “Input Actions”. I rename mine to “InputActions”.

On our InputActions object, we want to click the “Edit Asset” button, which brings up the Input Actions window. It is from here we can start creating actions.

Add a new action map and call it “Player”

We are now going to rename the current action in the list to “Movement” and change the Action Type to “Value” and the control type to “Vector 2”.

Right-click on our “Movement” Action and select “Add 2D Vector Composite” this will add 4 binding options to be used for moving up, down, left and right. Select our “Up” action and set the binding by going to “Binding” >“Path” and selecting “W[Keyboard]”.

You have two ways to search for this binding. The first involves typing “w” into the search bar and the second is by using the “Listen” button. Keep note that the Listen function doesn’t work on keys such as Escape.

Now we can map the other keys, go ahead and do this now. And tidy up the empty action “No binding>” in our actions list by right-clicking it and deleting it.

Remember to save the asset by clicking the “Save Asset” button at the top of the window.

Now we need to generate a new class based on our input actions. This is built into the object so there is no need to stress. Just click on our InputActions object and check the “Generate C# class:” checkbox and click apply. This generates the script to enable us to work with the actions.

Now we need to create an object to initialize our controller and manage our inputs. Create a new script called InputManager and add it to an empty game object which you can rename to InputManager.

The InputManager script needs to be turned into a singleton so all other scripts can access it. Go ahead and do this now.

We need to add a reference to our InputActions object and we need to enable and disable the controls as our InputManager is enabled and disabled and create a new InputActions on awake.

Now we need to add in some helper methods for our scripts to call so they can get the input values.

On our player script, we need to change our horizontal and vertical input variables to now get input from our input manager. We can access the x and y values by using “.x” and “.y” respectively when calling our helper methods and assigning the values.

Congratulations we now have a functioning player using the new input system.

Feel free to use the system you like. I will be continuing to use the new system from here on.

--

--

Luke Duckett
Luke Duckett

Written by Luke Duckett

🎮 First Nations Unity Dev from Wonnarua country 🏞️ | From Player to Lifelong Learner: Crafting Games, Debugging Code, and Embracing New Technology