Exploring Unity’s New Input System
Objective: Attacking using the new Input System
In this article, we will explore Unity’s new Input System to capture pressing the Space bar for attacking.
I’ll be incorporating the new Input System into my next prototype but feel free to use a new project with assets you like.
Create a Input Action asset file
Firstly I will create a folder to store the input action asset file and name the folder Input
Next, create a new Input Action and name it PlayerInputActions. To do this, right-click the Input folder > Create > Input Actions
Open the PlayerInputActions asset file by double-clicking on it. This will bring up a window.
In ActionMaps, create a new action map and give it the name of the character that is going to have this action. In this case, it’s my Spider.
Next, I am going to give the Spider its first action which is the Attack. Unity creates a binding for us to a button so I am going to bind it to the Space key by clicking on Listen and then pressing the Space Bar.
Click on the Save Asset button to save our changes.
Generate C# Class file from the PlayerInputActions which will contain all the events.
Create Player Input Script
Now, we are going to interact with this ActionInput using a script. Create a PlayerInput script.
Go ahead and open this script and add the following code. First, we need to include the InputSystem library.
I have created references to the input system and an animator
Next, we will initialise the variable, enable the action mapping and register the perform function
The function will call the trigger I created in my Animator that triggers the attack animation that you can see demonstrated in the animation below.
In the next article, we will look at the 2D vector option for the walking ability.
If you found this article helpful, please give me a Clap, as this brightens my day knowing I may have helped someone in their journey in making games. Also if you would like to see more, “Follow” me, so you will be notified of future releases. You may also send me a message if you need any further help.