New Unity Input System: Getting Started
In this Unity Input System tutorial, you’ll learn how to convert player input in your existing projects from the old Input Manager to the new Input System. By Ken Lee.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
New Unity Input System: Getting Started
20 mins
- 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 Actions
The new Input System provides four ways to handle action events.
In this tutorial, you used the SendMessages approach. You can change this option in the Behavior field in the PlayerInput component.
SendMessage
and BroadcastMessage
are the simplest ways to handle actions. When you use these two options, the system invokes the method with a name matching the name of the action.
For example, in this tutorial, the Jump action invokes OnJump()
and the Move action invokes OnMove()
.
BroadcastMessage
is similar to SendMessage
, except it can invoke the methods on any child GameObject. These two options are easy to use because you don’t need to configure anything to use them.
Using Invoke Unity Events
When using Invoke Unity Events, you configure the action much as you’d configure a button click in Unity UI.
This approach is more flexible, letting you use different methods in different objects. Those GameObjects don’t even need to have the PlayerInput
component.
Using Invoke C# Events
This approach is as flexible as Invoke Unity Events. You can define the methods you want to use instead of using methods with a specific name. However, if you use this approach, you need to write code to control which methods to invoke.
Here is a sample of how this looks:
private void Awake()
{
animator = GetComponentInChildren<Animator>();
rigidbody = GetComponent<Rigidbody>();
// 1
GetComponent<PlayerInput>().onActionTriggered += HandleAction;
}
private void HandleAction(InputAction.CallbackContext context)
{
// 2
if(context.action.name == "Jump")
{
HandleJump();
}
}
Here’s what this code does:
- Gets the
PlayerInput
component and registers the method toonActionTriggered
. - Controls which method to call for different actions.
Using the Update Cycle of the New Input System
In the old Unity Input Manager, you checked the input in every frame using Update()
. In the new Input System, you may wonder when actions are being sent, and if they’re sent before every Update()
.
The new Input System uses a different update cycle that’s independent of MonoBehaviour
‘s lifecycle. You can read more about it in Unity’s Execution Order documentation.
The system offers three Update Modes to control the update cycle. You can configure them in Project Settings ▸ Input System Package ▸ Update Mode.
Take a look at each of these nodes:
- Dynamic Update: Processes events at irregular intervals determined by the current frame rate. This is the default setting.
-
Fixed Update: Processes events at fixed-length intervals.
Time.fixedDeltaTime
determines the length of the interval. -
Manually: Events aren’t processed automatically; you process them when you call
InputSystem.Update()
. If you want a check similar to the old system, you can callInputSystem.Update()
inUpdate()
.
These new options, as part of the new Input System, give you a lot more control over input, whilst also making it easier to support multiple input devices :]
Where to Go from Here?
Download the completed project using the Download Materials button at the top or bottom of this tutorial.
In this Unity Input tutorial, you’ve learned:
- The basic layout of the new Input System.
- How to use actions and bindings.
- How to handle different kinds of player input efficiently.
To test your skill, try to add a Pause action to the game!
To learn more about the new Input System, you can read the Unity Input System manual.
I hope you have enjoyed this tutorial! If you have any questions or comments, please join the forum discussion below.