Unity's New Input System (+ How To Use It!)

Luis Ramirez Jr
Luis Ramirez Jr
hero image

Since the inception of Unity, the Input manager package has been the de facto standard for working with input from controllers.

However, times are changing, and Unity has been pushing developers to move away from the Input Manager and to start using the Input System instead.

What is Unity's new Input System?

It's really exactly as the name implies. It's Unity's newer system for capturing input from controllers.

unity input system

Now don't worry. It might seem intimidating to use at first because of all the new terminology and documentation to sift through, but I've got your back.

In this guide I'll walk you through exactly how + why to use this new feature along with what everything means.

Top 4 Reasons to use the new Input System vs the Input Manager

So, why would you want to use the input system over the input manager? There are four big reasons why.

  1. Better cross-platform support. Want to support Xbox? Playstation? Regardless of the platform or console, all types of devices can be used to play your games. This includes touchscreens and digital pens
  2. Event-based instead of poll based. Events consume fewer resources than constantly checking for input in the Update() function
  3. It's easier and faster to switch bindings to experiment with different gameplay styles to improve user experience. You don't have to dig through your code to switch inputs manually
  4. The debugger. A debugger is available with the new input system to help you check for inputs during runtime

Overall, the input system handles most of the heavy lifting for you. Sure, setting up the new system can take a bit of work, but it's well worth it as your game continues to grow.

In this tutorial, I'll show you the ropes of the input system so that by the end, you shouldn't have a problem binding basic inputs.

If you don't have a current project to test this on but want to have a go implementing this, then go ahead and check out this repository to use the same project as me.

Sidenote If you want to learn about other binding methods such as composite binding, or dive deeper into the input system and much more (such as how to build an actual game from scratch in Unity), then check out my Unity course.

learn unity as a complete beginner

I created this course to be the one and only Unity course you need to go from complete beginner (no coding experience) to coding your own 3D games and getting hired as a Game Developer!

Alright, let's get into the this Unity Input System tutorial.

Installing the Input System Package

The input system is only supported on versions of Unity 2019 and above. Preferably, it would be best if you were working on the latest stable version of Unity.

Once your project is opened, you can navigate to the Window > Package Manager menu item to view a list of installed packages on your machine.

In the top-left corner, there will be an option to view the Unity Registry. Select this option so that you can view a complete list of official Unity Packages.

Unity Registry

Next, search for a package called Input System and install it.

Input System Package

Voila! You've now got the input system installed in your project. The next step is to start binding actions.

Creating an Input Action Asset

The next step is to create an input action asset.

Input action assets contain a complete list of actions that can be performed with controllers. This way, you have everything in one central location for managing inputs.

You can create an input action asset like you would any other asset. In the projects window, you can right-click to create a new input actions asset like so.

Input System Package

From there, you can name the file whatever you want.

Then, double-click on the file to open the Input Actions window.

Input actions

This window allows you to start adding actions and bindings.

But what exactly are actions and bindings?

Terminology is half the battle when learning something new, so before we get any further into how to use this, let's break down what these terms mean.

  • Binding - A specific key or button pressed on a gamepad that should cause an action to be performed within your game. For example, pressing the T button may cause the player to attack
  • Action - Describes the behavior that will be performed in your game For example, running, attacking, jumping, or navigating a menu can be considered actions. Actions can have multiple bindings applied to them
  • Action Map - A group of actions. In some cases, you may want to use the same bindings for different actions. For example, you may want to use the WASD keys for in-game actions, such as moving around but also use them to navigate the UI menu

Adding an Action Map and Action

The first step to start managing your inputs is to create an action map.

Important. All actions are grouped into action maps, while all bindings are grouped into actions.

You can press the + button under the Action Map section. From there, you can assign any name you want. Next, you can start adding actions in the second column.

Adding an action map and action

In the above image, we've created an action map called Gameplay. If we were developing a game with land and sea maps, you might want to play action maps called Land and Sea. In addition, it's not abnormal to create an action map for the UI.

As for the action, it should accurately and briefly describe what the player will be doing when they press a specific key or button. In our example, we're preparing an action for attacking, therefore, our action is called Attack.

Action Types

The last step is to add a binding. However, before we do, we must consider the action type, which can be configured in the last column.

What is an Action Type?

An action type 'determines the process Unity goes through to track your actions'.

What does that mean?

Simply that controlling the player is not as easy as it might seem.

Instead, you need to consider the following:

  • Does the player need to press the button once for an action to be performed?
  • Do they need to perform button smashing? Or do they need to hold a button for an action to be continuously performed?

Based on your answer, you can then choose an action type that fits the needs, because choosing the right action type will make it easier to achieve the desired gameplay behavior.

There are three action types, called button, value, and pass through. Each has its pros and cons so let’s go through each of them.

Action Type: Button

The Button action type listens for a single button press. Emphasis on the word "single."

The button type does not care if the player holds down a button. Once the button has been pressed, the action is initiated. It is not initiated again until the button has been pressed a second time.

This behavior is perfect for most scenarios. For example, a player can pause a game with a single button press. However, it would be awkward to force the player to hold down a button for the game to remain paused.

In addition, this behavior can be desirable for games with button smashing.

For example

Mario party has mini-games that require the player to repeatedly smash a button to win. Choosing the button type will be based on what you’re trying to achieve in your game.

Button Mashing

Action Type: Value and Pass-through

The next two types are called value and pass through, and they’re very similar to one another.

Unlike the button type, these types will listen for a continuous button press.

For example

This behavior may be desirable for moving players around the screen. i.e the character should move and stay moving, as long as a button is pressed.

This brings us to the question, what’s the difference between value and pass-through?

Understanding Disambiguation

Unity performs a process known as disambiguation. Alternatively, you may hear this process referred to as conflict resolution.

Disambiguation is the process of deciding which controller should trigger an action, as the input system supports multiple devices at once. If the player has two controllers tied to their system, how does Unity know which controller should trigger an action?

Well, behind the scenes, Unity performs a few checks to make this decision.

Disambiguation is performed with the value and button types. However, it’s not performed with the pass-through type.

If multiple devices are triggering the same action, all of them will cause the action to be activated. This behavior is the most significant difference between pass-through and the other types.

Here's a table with a summary of each type.

Button Value Pass-Through
Single Press Yes Yes Yes
Continous Press No Yes Yes
Disambiguation Yes Yes No

Bindings

Let's shift our focus back to the project.

For the Attack action, I think it would make sense to use the button action type. This way, if players want to attack continuously, they must repeatedly press the same button for each attack.

Next, we can start adding a binding by pressing the + button to the right of the action.

Adding a Binding

There are different types of bindings we can apply, but for the sake of simplicity, adding a plain binding will suffice.

In the third column, there's a field called path. You can bind any key from any device with this, from keyboards to gamepads like Xbox controllers. There's an endless amount of options you can set a binding to.

To make things easier, there's an option to listen for a button press from a device connected to your PC which will then filter the results to that specific key.

For example

We can bind the Attack action to the Spacebar key on the keyboard if we so wished, and then faceroll our keyboards to do DPS 😝.

Binding the Spacebar key

That's it! You've successfully created your first binding.

Loading an Input Action Assets

Like most files in Unity, input action assets are not automatically loaded into your game.

Instead, an input action asset must be loaded via a component on a game object in our scene.

Here's how to set this up:

In your scene, create a game object called Game Manager.

It doesn't have to be loaded via a manager, but it can be convenient to have the input action asset loaded on an object that'll be available on all scenes, like a manager.

Then, on this object, add a component called Player Input.

Binding the Spacebar key

This component is provided by the input system package. It'll handle loading an input action asset via the Actions setting.

After selecting an input action asset, you can then set the default action map. However - only one action map can be active at a time!.

Handling Actions with Events

The last step in this entire process is to handle the actions from the input action asset.

When a binding is triggered, the corresponding action sends a message, which we can intercept via an event.

Here's how:

On the Game Manager object, add a custom component called InputSystemController. Here's what the component will look like.

using UnityEngine;
using UnityEngine.InputSystem;

public class InputSystemController : MonoBehaviour
{
    public void HandleAttack(InputAction.CallbackContext context)
    {

    }
}

Firstly, we're including the UnityEngine.InputSystem namespace for the classes and structs corresponding with the input system.

Next, we're defining a method called HandleAttack. The most noteworthy thing about this method is the context parameter. It's annotated with the InputAction.CallbackContext type.

This type contains information related to the button press, and a complete list of properties can be found here. There are also methods you can call for reading the value.

In most cases, you will want to confirm the button is pressed.

public void HandleAttack(InputAction.CallbackContext context)
{
    print(context.phase);

    if (context.performed)
    {
        print("Attack performed");
    }
    else if (context.started)
    {
        print("Attack started");
    }
    else if (context.canceled)
    {
        print("Attack canceled");
    }
}

Each action has three phases.

  • Started - The button press has been initiated.
  • Performed - The button press has been successfully performed. Runs immediately after the started phase.
  • Canceled - The button has been released.

Each of these phases has a property dedicated to them that store a boolean value. Alternatively, you can check the phase property containing the string form of each phase.

Registering a Method with the Event

Back in the Unity editor, the Player Input component allows you to register a method with an event. Custom events are created for action.

First, the Behavior setting must be set to Invoke Unity Events.

Behavior

Afterward, a list of events is presented for each action. If you were to look inside the events, you'd eventually find an event for the Attack action.

You can add a method by clicking the + button, adding the game object, and then searching for the method in the dropdown. In this case, the method exists on the same object under the Input System Controller component.

Behavior

If you were to run the game, you should find the following in the console.

Action Phases

Key Takeaways

Overall, the input system is pretty powerful for listening to key presses, while the ability to centralize all actions into one file provides an easy way to add bindings.

However, we've only touched the tip of the iceberg! Composite bindings are another popular type of binding you can add to actions.

I mentioned this up top, but if you want to learn about other binding methods such as composite binding, and dive deeper into Unity and learn how to build a complete game from scratch in Unity, then check out my Unity course.

learn unity as a complete beginner

It really is the only Unity course you need to go from complete beginner, with zero coding experience, to coding your own 3D games and getting hired as a Game Developer this year!

More from Zero To Mastery

How To Move Game Objects (With Physics) In Unity preview
How To Move Game Objects (With Physics) In Unity

Want to add movement to your game objects but not sure how to get started? In this tutorial, I share 2 simple methods AND a follow-along practice project!

How To Get Started In Game Design preview
How To Get Started In Game Design

Want to go from playing games to getting paid $100K+ to build them? Our Game Design expert shares the steps to go from total noob to hired as a Game Designer.

[Guide] Computer Science For Beginners preview
[Guide] Computer Science For Beginners

You DO NOT need a CS Degree to get hired as a Developer. Learn Computer Sciences Basics today with this free guide by a Senior Dev with 10+ years of experience.