Unity Input System Basics
Here I want to go over Unity’s new input system, from setting it up to getting basic interactions going. We will focus on keyboard interaction in this article.
Let’s first talk about setup. The first thing you need to do is to open the Package Manager and import the Input System into your project. If you don’t remember how to do that, here’s how:
Per the installation documentation, the new Input System requires Unity 2019.1 or later and the .NET 4 runtime. We will check the runtime in a moment. Refer here for the documentation:
Next you need to go to the player menu in the project settings and check that the new system is enabled. We can also check our .NET runtime version here as well.
Under configuration, you will see the Active Input Handling drop down. It has 3 options, the old system, the new system, and both. When this is changed, Unity will require a restart. The new system should be preferred moving forward, however, the old system is still good for rapid prototyping. Also check the API compatibility level for .NET 4.x.
Once that is done, we need to create a new InputActions item in our asset folder:
I renamed it to PlayerInputActions. Double click on it to open the actions map as seen below:
Next we can create action maps. For example, if I want an action map to move the player around I can create one by clicking on the plus next to Action Maps:
You can create multiple action maps for different situations. For example, you might have another key binding (action map) if the player enters a vehicle or for a pause menu. Only one map can be active at a time and they can be switched dynamically at runtime.
Now we need to add some actions to our player map. I’ll start with adding a fire command when the player presses the space bar:
Below we add a binding, then choose the keyboard under path. From there if we click on Listen we can then press the key we want to use and it will populate in the menu below Listen.
Now we can add another binding for movement. Here we will add a standard WASD movement scheme. To do this we will use a value action type and a vector2 for control type:
Next we add our binding. We will choose Up/Down/Left/Right composite:
Now we can record what keys we want to control movement. Here I will start by assigning the W to Up:
Continue doing the same for the other keys, until all 4 are assigned. Also don’t forget to save your asset from time to time at the top of the action map window.
Lastly I want to set up a quick save key combo. We will use the left control key combined with F5 to demonstrate how a quick save can be setup. Here I will set up the action and a binding with one modifier:
Now just assign the keys to the binding like normal:
Now we will hook this map up to some code to get this working. First click on generate C# class and then hit apply. This will generate code corresponding to your action setup. You don’t need to worry about it for now. Also, anytime there is a change this code will be updated.
Now we need to create a player class to handle input:
Basically we need to do 3 things. We need to create an instance of our PlayerInputActions, which was generated when we clicked generate C# class. Then we need to enable the action map we want. Here we are enabling player. Remember there can be multiple action maps. Lastly we assign a method to handle this input and assign it to the “performed” callback. There are other callbacks like “started” for when the key press starts and “cancelled” for when the key is released. We will just use performed for now, which triggers per keypress.
Here I am printing out “Fire” on the console when the space bar is pressed.
Lastly, we will cover movement. The way we set up our movement input above allows for standard WASD movement. When we push one of the keys the input system will return a vector2 with values that are dependent on what key we pressed. This is how we set it up above. For example if we press “A” the vector2 would be (-1,0). If we pressed “A” and “S” together we would get (-1,1), meaning we are moving left and up. If we read the value when no keys are pressed the value will be (0,0).
All we need to do is read the movement value in our Update loop and translate the player by the vector value. Like so:
This will give us the below smooth movement:
That’s all it takes to get basic movement up and running. For another quick example let’s look at a 3D movement scheme. For this, I am going to read the Vector2 and then create a 3D vector using that information to allow the player to move forward and back, the way 3D movement should work. This is done with a quick change to the update method:
Above we read the vector2 and then create a vector3 using the x value for the x and the y value for the z. Y in the vector3 is set to zero. In reality we would most likely have additional keys set up for up and down movement, say, for example, if the player can fly or change altitude. This update performs as follows:
The movement was accomplished by pressing the WASD keys.
Thats it for a real basic treatment of the new Unity input system using the keyboard.