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.
It's really exactly as the name implies. It's Unity's newer system for capturing input from controllers.
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.
So, why would you want to use the input system over the input manager? There are four big reasons why.
Update()
functionOverall, 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.
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.
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.
Next, search for a package called Input System and install it.
Voila! You've now got the input system installed in your project. The next step is to start binding actions.
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.
From there, you can name the file whatever you want.
Then, double-click on the file to open the Input Actions window.
This window allows you to start adding 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.
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.
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.
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.
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:
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.
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.
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?
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 |
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.
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 😝.
That's it! You've successfully created your first binding.
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.
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!.
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.
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.
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.
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.
If you were to run the game, you should find the following in the console.
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.
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!