Page cover

Input system

To move the player around on the world map and make the player confirm the level they want to enter, we need to implement that ability to the player by making use of the new input system of Unity.

If you want to learn more in depth about how the input system works and what else you can do with it outside the scope of this project, click on the link below to learn more.

For this project I'm going to make the controls work universal, meaning if you're playing on either a PC with mouse and keyboard or with any type of game console controller, the input system will translate that input so you will always get the same outcome as you would get from using any other type of input. For example, you can start out playing the game with your mouse and keyboard and want to switch over to an Xbox controller, the input system will recognize the change and you're still able to play the game without having to restart it.

Adding the input system

Let's start off by adding the new input system to our project. We do this by going to Window --> Package Manager.

Now that you have the package manager window open, click on the drop down menu where it says "Packages: In Project" and then click on "Unity Registry".

From there go the search bar and type in "Input System" and then press install.

Due note that the input system version shown in this tutorial may be out-dated. Nevertheless the same method of implementation still applies.

When pressing the install button you'll see a pop-up window telling you that the project is using the new input system, but not the native platform back ends, because they are not enabled. Select the "Yes" option to enable the back-end option and let Unity restart.

After Unity restarts the new input system is now fully implemented into your project. You can confirm this by going back to "Packages: In Project" and see the "Input System" in the list of packages.

Creating the input actions asset

In order to actually use the new input system we must create an Input Actions Asset. This asset will store all the input configurations we want to use in our project. To create an Input Actions Asset you right-click into the folder structure, then go to Create --> Input Actions.

I like to keep my input actions in a separate folder so I can easily find it back again, but it's up to you where you want to save it for your project.

Once created, you can name it to whatever name you want it to. I've named mine "Player Controls" so I know what this input action asset is used for.

Configuring the input actions asset

Now that we've created and named our input actions we can now open it and we'll see the following window.

From here we can press the "+" icon next to the "action maps" text to create a new action map. The result looks like this.

You can name your action map however you want. Next to the "Action Maps" is the "Actions" that holds all the different actions you want your player to have. Attached to an action is the "Action Properties". In here you will set what type of action it is or, if chosen any other option besides the default "Button" one, a control type with a list of different options on how this action should be read by Unity.

For this tutorial we want to move the character from level node to level node on the world map. The choices the player has is going to be either up, down, left or right. In Unity that would be a 2D movement across the X and Y coordinates, so a Vector 2 should be used to get this outcome.

I like to name my actions accordingly to what they do or represent, in this case its movement across a world map. So I'm going to call my action movement. Then I'll set the action type to "Value" and the control type to "Vector 2".

If we press on our new action we'll see that we have added no bindings to this yet. Bindings are used to easily configure different inputs types to the same action.

For this example, I shall make one that works with a keyboard and another one that handles any controller type.

First, we delete the empty binding that came automatically generated by the action. Now we right-click on the action and select the option "Add Up\Down\Left\Right Composite" and you'll now see the following addition.

You can name the binding any way you want. I like to name them to what keys or controller type inputs they represent. So for this example I named my WASD, since those are the keys I want to use for my project.

To add the specific key or controller type input to one of these options, simply press one of them that says "No Bindings" and you'll see under the "Bindings" options that the path is empty. This means we haven't set what type of input we want to correspond to our selected option.

Once you press on the empty path drop down menu you'll see an option to listen for an input, search for an input or different input types where you can select your input type option from.

If you want to your project to use a controller, use the "Gamepad" option and NOT the specific controller you are currently using. The reason behind that is the game pad option works for all different types of controllers, when only selecting your specific controller does not.

I suggest to configure the controls per option however you see fit. For this example, I'll add my keyboard configuration as mentioned before.

If you want your game to be controlled by multiple input types just follow the previous image above to configure the other input types the same way. This is how my example looks like.

Now that we have our movement set up we only need to add one more input, the confirm button. This button will allow us to enter the level we are currently standing on when pressing the button.

To add this button we need to add a new action. Just like before you can name this however you want, for this example I named mine "Confirm".

Just like we did with the movement action we need to configure this action to do what we want it to do. Luckily for us the default setup is just how we needed it to be, we only need to do some small modifications.

When we look at the action properties there is another option called "Interactions". What interactions allow us to do is to set up how the button interaction gets registered. If we press on the "+" we see the following options.

We want to use the "Press" interaction and leave everything to the default settings. Now we start to add the binding or bindings to this action that will handle our confirm press. Similarly as before, I use the enter key in this example for the confirm action.

You can add as many input types to this as well, just like we did for our movement action. In my example I've added the "Button South" of the game pad to this as well.

For those who don't know where button south is on your controller, it's on the right side of your controller. With Xbox it is represented by the A , Playstation by the X, etc.

Now that we've configured all our settings it is time to save them. You can manually save the asset by pressing the "Save Asset" option or check mark the "Auto-Save" box so you don't have to remember to press save.

My advice is to check mark the auto-save function because if you've made a lot of changes, or added a lot of new stuff and forget to save before pressing close, all those changes will be lost.

NOTE: When you check mark the auto-save option Unity will start to automatically keep saving every change you've made to the input asset, no matter how small the change is. This may cause some lagging issues when running this on a system that has very low specs.

Last updated