Scripting Actions in Unity’s New Input System

Objective: To understand how to script an Action using Unity’s New Input System.

Buddy the Elf completing his ‘Jump’ Action — tenor.com

As we have now setup our first complete Action Map (with Actions and key-bindings) using Unity’s New Input System, we can move onto learning how to script an Action.

Generating a C# Class to Access Our Events

The first step is to generate a C# class that is going to give us access to all of our Actions and key bindings that we setup in the Action Map. Luckily for us, Unity could not have made this any easier for us as we just need to click on the tick box for ‘Generate C# Class’ when you have the ‘PlayerInputAction’ asset highlighted in the Inspector window. This will generate some automatic fields which we can leave as is:

Generating a C# Class

Next hit the ‘Applybutton, which will generate a C# script in your Project window with the name that was in the ‘C# Class Namefield:

Automatically generated C# class for PlayerInputAction

There is no need to do anything with this script, and in-fact can cause issues if you attempt to modify it. But, if you open it up and take a scroll through, you will see that it contains our Action Map, Actions and the key-bindings (with relevant paths, interactions, processors etc..) associated with these Actions. It also shows the ‘Action Type’ of an Action, whether it is a ‘Value’ with a Vector 2D Control Type’ or a ‘Button’ etc.

Basically, everything we need has been automatically generated in this script for us, which we simply need to reference to be able to access all this information. So let’s go head and do this!

In order to interact with our Action Map, we now have 3 steps we need to take:

1. Create an Instance of the Generated C# Class

Getting a reference and creating an instance of the PlayerInputActions class

Create a new C# script called ‘PlayerController’, dragging this onto a Player game object in the scene. Open this up and let’s create an instance to our PlayerInputAction C# class in the Awake() method.

2. Enabling Our Actions/Action Map

Next, for an Action to be able to do something, we need to first enable it. We will do so by enabling our Action Map in the OnEnable() method. As good practice we should also be disabling this in OnDisable().

It is also possible to individually enable Actions as you see fit. Enabling Action Maps as a whole, is more efficient than individually enabling Actions.

Enabling and Disabling our ‘Player’ Action Map.

3. Register Actions

Lastly, we can now register our Actions to events such as ‘Performed’, ‘Started’ or ‘Canceled’, which provide us with a bunch of very useful statistics as a Callback:

Registering the Jump Action to events — Started, Performed and Canceled

When we use the += signs in this context we are adding the method to the event.

Tip: After you have typed ‘+=” you can tab to auto complete and it will automatically generate a method for you.

Now that we have registered our ‘JumpAction to the perform, started and canceled events, anytime we hit the ‘Spacekey it will run the methods that are registered to the events.

Let’s see the Unity Console when we run this and hit the ‘Spacekey:

Unity Console window callback output for JumpStarted, JumpPerformed and JumpCanceled methods

Looking over the information the Callback has provided us, we can see that there is:

  • An Action Map — (“Player”),
  • An Action— (“Jump”),
  • A key-binding that was pressed — (“Keyboard/Space”),
  • A Phase it was in — (“Started”, “Performed” and “Canceled”),
  • What Time it was when the event occurred — (“4.906”, “4.906” and 4.974"),
  • A Value, either 0 or 1 for keyboard input — (“1”). There is only 2 states this can be in. 1 reflects that a key is/has been pressed, and conversely, no buttons are/have been pressed for 0.
  • The Interaction. But in our case we don’t have an interaction setup.

The events for ‘Started’ and ‘Canceled’ can be compared to the old input system with ‘GetKeyDown’ and ‘GetKeyUp’.

We have now successfully scripted an Action in Unity’s New Input System!

Next Article

“Exploring the Different Actions Types in Unity’s New Input System”

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store