Unity’s New Input System

Siddhant Thakur
Nerd For Tech
Published in
6 min readMay 12, 2021

--

Unity’s new input system is more like a modular input system. The idea behind this is, we define actions, code the behavior for those actions, which are then triggered by our input devices. Implying that our code no longer needs to be coded independently to use controllers, Keyboards, and mice (you can have different key-bindings for different devices to trigger the same action). The new input system makes it easier to add multi-platform support to our games.

Let’s start by adding the new Input System into our project, using the Package Manager(Make sure you are in the Unity Registry while searching for the Input System package).

Window → Package Manager → Unity Registry → Install Input Systems

Upon completion, you’ll be asked to restart the project to enable the newly installed Input System.

Yes!!!

Note: With this, you won’t be able to use the old Input System. To switch back or have both new and old Input Systems function while you switch the codebase to match the required system, you can do the following,

Edit → Project Settings → Player → Other Settings → Active Input Handling

For starters, let’s create an Input Action asset that will store our desired Action along with the required keybindings for various devices.

Let’s take a good look at the Input Actions panel,

We’ll be making a basic character movement(move up, down, left, right, and diagonally) to showcase the new Input System.

So, we can have an Action Map that deals with all kinds of Player controls, implying that the Action here would be the Movement and the Properties would define the movement further.

Now let’s work on adding Keyboard and Controller bindings to our movement action. For Keyboard, I want to use the W, A, S, D keys to move Up, Left, Down, and Right, respectively. And for the Controller, I want to use the left analog stick to control the movement.

Since we want the movement to occur continuously, as we press the desired key/move the analog stick, we need to manipulate the Action Type property.

Action Type:

Value: Used when continuous state changes are needed to be tracked(For example, Movement). If two devices are being used simultaneously, the one which influences the output more is returned.

Button: This is the default setting. It is used for tracking key or button presses(For example, press X to Jump).

Pass Through: This is essentially the same as Value, with the only difference being that when two or more devices are used simultaneously. Pass Through doesn’t check which influences the output more. Instead, any influence is returned(most recent influence, irrespective of how much influence it has).

For this test case we’ll be using the Value Action Type.

When working with Value or Pass-Through Action Types, we find ourselves with an additional parameter called Control Type, which defines the type of Input we expect from our Input Devices. Since we are dealing with vertical movement, horizontal movement, and a combination, we’ll be going with a Control Type of type Vector2.

The Properties tab also contains an Interactions and Processors option. Interactions deal with conditions that need to be satisfied for the input to trigger (hold down a button, click a button, multiple presses, and more). Processors modify the input value when received (Normalizing values, inverting values, setting deadzones, and more).

We can now add the keyboard and controller Actions that are the key bindings.

When we create an Action(Movement in this case), it comes with a Binding(yet to be configured). I’ll use that to configure the Joystick and create a new 2D composite vector used for the Keyboard bindings.

Note: “Listen” finds the input you just pressed. So, if I move the left joystick, I see two options, one being Left Stick [Gamepad] (The one I chose, as I want any controller to be able to move my character) and the other the [Playstation 4] that is the controller I’m using.

I can also create a Control Scheme, which categorizes my controls based on the input devices used.

I’ve created two control schemes, one for the Keyboard and the other for the controller. Now let’s group our Actions based on our control scheme.

Now all that’s left is to save our Asset (I’d recommend having Auto-save toggle ON).

With this, we have the player inputs configured. Let’s add this to the Player.

Here, we added the Player Input component to the Player game object. This component requires an Input Actions asset (The one we created above). We can set the default control scheme here, also have an auto-switch between those schemes when input from respective devices is received. Lastly, this also contains a Behavior property that has the following options,

The Behavior field is responsible for determining the way that the Player Input notifies if something has occurred. Here, I’ll be using the Invoke Unity Events option. Allowing us to view all our Actions in the inspector and directly attach our method callbacks to them. This is also Unity’s recommended method. But at the end of the day, it comes down to preference. I’ll be using the recommended method for this article.

Let us move on to coding the Player behavior.

Since we’re using the new Input System, we’re required to import the Input Systems package to use its functionalities.

With this, we can now use all the methods defined in that package.

Where Movement() is called in the Update(),

Now all that’s left is to attach this behavior to the Player game object. With this, we can add it to the Movement Action we created at the beginning. We can now reference the Direction(). Thus, linking everything together. That is, We defined an action (Movement Action created at the start), we coded its behavior (Direction(), in the behavior script), which triggered by the inputs (linked into Actions as shown at the start).

With this, we should be able to move our character using a keyboard or a controller.

Pay attention to where the mouse cursor is. You should see the Control Scheme change dynamically from keyboard to controller( it even shows the type of controller used).

Note: Another benefit of using the new Input System for the Keyboard control scheme is that the output values by default are normalized.

Thank you for reading, More to come!!!

--

--

Siddhant Thakur
Nerd For Tech

Aspiring Game Developer with experience in the field of Machine Learning.