New Unity Input System: Getting Started
- Getting Started
- What’s New in the Unity Input System
- Simpler Action and Binding Architecture
- Gathering Information With the Input Debug Tool
- Support for Multiple Devices and Platforms
- Understanding the New Input System
- Installing the New Input System
- Creating an Input Action Asset
- Setting up the Action Editor
- Creating a Jump Action
- Implementing the Jump Logic
- Linking the Jump Action to the Code
- Creating the Move Action
- Implementing the Move Logic
- Handling Actions
- Using Invoke Unity Events
- Using Invoke C# Events
- Using the Update Cycle of the New Input System
- Where to Go from Here?
Handling input is a pillar of creating a successful game. Input devices help player characters perform actions inside the game such as walking, jumping and climbing.
Recently, many new platforms have come out, introducing more Unity input devices. These include touch screens, VR controllers and gamepads from different game consoles. If you want your game to support different platforms, you need to write code to handle the logic for different devices. The more platforms you support, the more complex your code will be.
Luckily, there’s a new Unity Input System that helps developers deal with this situation, making input-related code easier to manage.
In this tutorial, you’ll learn:
- The features of the new Input System.
- How the new system works.
- How to migrate apps to the new Input System from the old Input Manager.
The materials for this tutorial were built in Unity version 2020.1. You can get this version of Unity from the Unity website or install it with the Unity Hub.
First, download the starter project for this tutorial using the Download Materials button at the top or bottom of this page. Unzip its contents and open NewUnityInputSystem-Starter in Unity.
After the project loads, you’ll see the RW folder in the Project Window:
Take a look at the organization of the folder:
- Fonts: Fonts used in the scene.
- Materials: Materials for the scene.
- Models: 3D meshes for the player character and game environments.
- Prefabs: Pre-built components composed of Scripts and Models.
- Scenes: The game scene.
- Scripts: Scripts with game logic.
- Settings: The settings file, which is where you’ll put the input settings.
- Shaders: Shaders for special effects like the player’s shadow.
- Textures: The graphics used by Materials and UIs.
The starter project is a simple platformer game. You control the player character by moving around and jumping to collect coins.
The game is ready to play. Open GameScene from the Scenes folder and click Play to try the game for yourself!
Currently, the Move and Jump controls use the old Unity Input Manager. You’ll learn how to use the new Input System to replace the old system later in the tutorial.
What’s New in the Unity Input System
Before diving into the code, take a look at the new Input System’s features.
Simpler Action and Binding Architecture
The old input system checked input from different devices every frame to determine whether players took an action.
The following code, which supports both gamepads and keyboards, is an example of the old way of doing things:
bool isGamepad = Input.GetAxis("Gamepad Fire") != 0f;
return Input.GetAxis("Gamepad Fire") >= triggerAxisThreshold;
The code uses
if-else branching to handle support for different devices and their associated actions.
The new Input System separates device input from code actions. That means you only have to handle the actions the players trigger. You don’t need to know which device the player is using or which button they’re clicking.
An input event in the new system is called an action, while the mapping between an action and an input device is a binding.
Gathering Information With the Input Debug Tool
The Input System provides you with a new tool called Input Debugger. Open it by selecting Window ▸ Analysis ▸ Input Debugger from the menu.
The Input Debugger helps developers by gathering the following information in one place:
The state of the Input System, including:
- Device: Information about the connected devices.
- Layout: Which controls those devices provide.
- Setting: The configuration of the input system.
It also provides real-time information about a specific device. Open this by double-clicking the device from the device list in the Input Debugger window.
Here’s a demo of the Input Debugger in action:
Feel free to keep the Input Debugger open while you work through the tutorial.
Support for Multiple Devices and Platforms
With the increased flexibility from the Input System’s new architecture, Unity can support many different input devices, including:
Understanding the New Input System
The new Input System has four building blocks that funnel events from player devices to your game code:
- Input Action Assets: A settings file that contains the properties of the actions and their associated bindings.
- Actions: Actions define the logical meanings of the input. You can find that information in the Input Action Assets.
- Bindings: Bindings describe the connection between an action and the input device controls. You’ll find this information in the Input Action Assets, too.
- PlayerInput: PlayerInput is a script that manages and links action events to the corresponding code logic.
Sometimes it’s easier to understand a new workflow if you can visualize it, so take a look at the image below:
Break this down into its simplest steps:
- First, the Unity Engine collects information from the connected devices and sends corresponding events, like a button click, to the Input System.
- The Input System then translates those events into actions, based on the action and binding information stored in the Input Action Assets.
- It then passes the actions to the PlayerInput script, which invokes the corresponding methods.
Now that you know a little more about how the Input System works, you’ll use it to control the game character in the coming sections.
Installing the New Input System
The first thing you’ll do is install the new Input System package. The standard Unity installation doesn’t include it.
Open Window ▸ Package Manager in the menu bar. Make sure that you select Unity Registry in the Packages dropdown, if you haven’t already.
Find Input System on the list. Select it and click Install.