Using the new Unity input system
Handling user inputs is paramount for efficient game development. Unity provides two built-in systems for handling inputs. These two systems are referred to as the old Input Manager and the new Input System.
Unity’s new Input System is an upgrade to the old Input Manager. It was officially released recently addressing the limitations of the input manager. It may appear difficult to use. However, it offers more flexibility and is safer to use once you get the hang of it.
In particular, the new Input System works seamlessly across various platforms. As a result, you can save time working on your project and focus on developing a great game instead of wrangling the input manager.
In this article, you will learn how to set up and use Unity’s new Input System for input controls in your game projects.
How to install the new input system
Let’s start by installing the new Input System in your project.
New Input System Requirements
Note that the new Input System has some requirements that your project must meet in order for it to function. You probably meet these requirements, but I’ll list them here for you.
- Unity 2019.1 or newer
- .NET 4 runtime
If you have met these requirements, you can install the new Input System with this step-by-step guide.
Install the new Input System in Unity
- In the menu bar, click Window > Package Manager.
- In the Package Manager, click Input System from the list.
- Click Install.
📄 Resources:
After installing the package, you will be asked whether you want to enable the new input backends. Click Yes to enable the new input backends and disable the old ones.
Now that you have successfully installed and activated the new Input System, we will explore how you can use it for input controls.
How to use the new Input System to get input from a device
You have two options when using the Input System to gather input. You can either read the current state of the input, or you can use the Input Actions system to hook into any updates.
Read the current Input System state
The quick and dirty way to gather input is to read the current state of the input device directly with a script. In this example, we bind the space input from the keyboard to a jump action. This is an implicit binding. Your Update method continuously polls the Input System and checks if the space input was pressed in the frame. If it was, then the action is performed.
using UnityEngine;
using UnityEngine.InputSystem;
public class GetInput: MonoBehaviour
{
void Update()
{
if (Keyboard.current.space.wasPressedThisFrame)
{
// Make the player jump
}
}
}
In the example script above, the new input system was used to retrieve input from the keyboard to trigger an action.
In reality, this is not the recommended method for using the new Input System. The ideal method is to use the Input Actions system to handle user inputs, then include a listener in your component that catches the Action and performs some behavior. This approach is safer and more reliable than the old Input Manager.
📄 Resources:
- Indie game programming for complete beginners
- Dictionary initialization C#
- Cannot assign void to an implicitly-typed varaible
How to get input using Input Action
The first thing you have to do is to create an Input Action asset in your project. Just right-click Create and click Input Actions. This will create a new Input Actions asset. Now, after doing that, you should define the input actions you need like; Jump, Fire, Shoot, and Move, and the associated input controls may be gamepad, keyboard, or mouse. You can do this on the Input Actions editor window. Note that you can assign multiple input controls to an input action.
With this, you have successfully created Input Actions to handle user inputs for your game projects. You can reference the input actions in your code using the PlayerInput component to trigger input events.
Here’s a basic example code of using Input Actions to handle input events in Unity;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
private PlayerControls controls;
private void Awake()
{
controls = new PlayerControls();
controls.Gameplay.Move.performed += ctx => Move(ctx.ReadValue<Vextor2>());
}
private void OnEnable()
{
controls.Gameplay.Enable();
}
private void OnDisable()
{
controls.Gameplay.Disable();
}
private void Move(Vector2 direction)
{
//Move player based on input direction
}
}
This is just an example code of moving characters in a game with Input Actions. Make sure you have defined the actions in your Input Actions asset before including it in your C# script.
Conclusion
You have learned how to use the new Input System to effectively handle user input in your game projects. Visit the Unity Docs for more information on using the new Input System.