In this blog we will talk about the new input system for Unity. In every game, one of the first elements to be programmed is the Input system, since it is in charge of detecting the player’s orders. The New Input System is the most practical way to enter different input devices in the development of our game without having to program one by one, so every developer should know it to determine if it is necessary to use it in their projects.
In this first blog about the new Input System I will show you the installation and a way to configure an action using this new system.
- What is the new Input System (NIS)?
- Installing the NIS
- Setting up the NIS
- Setting the Player Script
1. What is the new Input System (NIS)?
The NIS is a system that makes it easy for the developer to create relationships between one or more input devices and a response or action in your game. Its advantage lies in the ease of including various devices quickly and safely.
2.Installing the NIS
The NIS requires Unity 2019.1 to work so if necessary you must download a newer version from the Unity page
To install it go to Window> Package Manager
Where it says “Packages” look for the option “Unity Registry” and look for the one that says “Input System 1.0.2”, then click on “Install”.
Now Unity will inform you that it must be restarted in order to change the “old input system” for the NIS
Click on “Yes”
Up to this point all the tutorials that you will find will be the same, however, from now on there may be differences when configuring the NIS, in this blog we only present one way.
3.Configurando el NIS
Inside the Assets folder, right click and go to Create> Input Actions
With this we create our “Input Actions Assets” for this example we will call it “NewControls”. Clicking on this element wil make that the inspector presents us with the options of “Edit asset”
By clicking on “Edit Asset” a new Window appears which has 3 columns:
The first is “Action maps” where we define the ways in which different actions will be grouped.
The second column is “Actions” where we determine which is the device and the action that will take place in each action.
The third column is “Properties” where we will give more clarity about what each action will do and how it will work.
Also in the upper right we find the option “Auto-save” which can be activated, however, it will slow down the process of configuring the action maps. Do not forget to always save after you have made any changes.
By clicking on the cross in the column “Action map” we can add a new action map (in this case called “Player”) within which the actions will be defined.
Then in the column of “Actions” we can create new actions by clicking on the cross, we change the name of our first action to “Attack” and pointing to “No binding” we can define which will be the “binding” between the device and the action that triggers the “Attack” action.
In the column “Properties” in the part that says “Path” we define which is the input that triggers the determined action, a quick way to do this is by clicking on “Listen” in such a way that the next key that we press can be chosen as the trigger of the action.
In the image below we can see how our “Action map” called “Player” has an “Action” called “Attack” associated with the “Space” key so that pressing it activates the action.
Once finished with that (with the “Input Action Asset”, “NewControls”, selected) we click on “Generate C # class”
3 elements will appear below this, where we can determine the names of the Scripts that are going to be generated (as a recommendation it is preferable not to modify them). Now click on “Apply”.
This will create a new script that for this example is called “NewControls”
4. Setting the Player Script
For this in the hierarchy we create an empty object and then to this object we add the component “Player Input”
Now we drag the “Input Action Asset” called “NewControls” to the space called “actions” in the “Inspector”
We create a new Script called “InputPlayer” and add it to our object called “Player”
In the script we have to add “using UnityEngine.InputSystem;”
And then we write what appears in the image below so that the word “Attacking” appears on the console when pressing the “Space” key.
We use an “if” so that when pressing the button, in this case “Space”, it only runs the code once, otherwise it will run 3 times (since each call does the following actions: started, performed and canceled)
To finish in the inspector in the component “Player Input” in the option “Behavior” we indicate “Invoke Unity Events”
Now click on the arrow of “Events” then on the arrow of “Player”, we look for the option “Attack (context callback)” we drag our object “Player” and we look for the option InputPlayer> Attack
In the image below we see how it looks
Now you can enter Playmode and verify that “Attacking” appears on the console each time we press the “Space” key.”
With this we finish our first blog about the New Input System, in the next week we will explain how to configure the player’s movement.
As always thank you so much for reading our blogs!!
If you find this blog useful let us a comment, and follow us in our Instagram @tortugamedev and Twitter @Totuas2.
And don’t forget to check out the rest of our blogs about game development and our games!!