The new Input System of Unity makes a pretty big shift for developers, adding more flexibility, power, and customization to how you can handle input in your games. This is a comprehensive guide that will take you through all of the ins and outs of the new Input System, equipping you to master all the features and implement robust input handling in your Unity projects.
Understanding the New Input System Architecture
Before jumping into implementation, let’s familiarize ourselves with the basic building blocks of the new Input System:
1.1 Input Actions
Input Actions are the core of the new system. The Input Actions represent high-level input events like “Jump” or “Fire”, which your game responds to, regardless of the particular device or control that triggered them.
1.2 Input Action Assets
Input Action Assets Define groups of Input Actions. So, one Input Action Asset may specify keyboard controls, another control scheme for a gamepad, and yet another for a touch screen. One of the benefits of having an Input Action Asset is that it is pretty easy to edit in the Unity Editor.
1.3 Input Devices
The system of Input Devices represents physical input devices like keyboards, gamepads or even touch screens. The new system automatically discovers and handles the connected devices.
1.4 Input Bindings
Input Bindings associates input actions with specific controls in input devices, such as binding the “Jump” action to the spacebar on the keyboard.
1.5 Input Processors
Input Processors are entities that enable you to transform input data as it reaches your game’s logic. An example of an Input Processor is the smoothing, inverting, or scaling of input values.
2. Setting Up the Input System
To get started with the new Input System, follow these steps:
- Open your Unity project and go to Edit > Project Settings > Player
- In the Configuration section, find “Active Input Handling” and set it to “Both” or “Input System Package (New)”
- Unity will prompt you to restart the editor. Click “Yes” to apply the changes
- Install the Input System package via Package Manager if it’s not already installed
Note: Setting Active Input Handling to “Both” allows you to use both the old and new input systems simultaneously, which can be helpful when transitioning existing projects.
3. Creating and Configuring Input Action Assets
Input Action Assets are central to organizing your input scheme. Here’s how to create and configure one:
3.1 Create Input Action Asset
- Open Project window, click in it and right-click, then you need to select Create > Input Actions.
- Input a name of your new asset, for example “PlayerInput”
- Double-click the asset which will open up an input actions editor
3.2 Defining action maps
Action Maps is the sum of actions collected within a small group. For instance, you will have different maps for “Gameplay”, “UI”, and “Menu” contexts.
- Open Input Actions editor, click on the “+” button next to “Action Maps”
- Name your new Action Map (for example “Gameplay”)
3.3 Creating Actions
Within each Action Map, you can define special actions:
- Select your Action Map
- Click on the “+” button next to “Actions”
- Name your action (for example “Move”, “Jump”, “Fire”)
- Set the action type (Value, Button, or Pass-Through)
3.4 Adding Bindings Bindings attach actions to specific inputs:
Bindings connect actions to specific inputs:
- Choose an action
- Click the “+” button to the left of “Bindings”
- Choose the appropriate type of binding (for example “2D Vector Composite” for movement)
- Configure the properties of the binding in the inspector
4. Using Input Actions in Your Scripts
To use your defined input actions in your scripts you have to generate a C# class from your Input Action Asset:
- Select your Input Action Asset in the Project window
- Finally, in the Inspector, click Generate C# Class
- Click “Create” to create the class
You can use this class as generated to your scripts. Here’s an example that applies player movement and actions:
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
private PlayerInput playerInput;
private Vector2 moveInput;
private bool isJumping;
public float moveSpeed = 5f;
public float jumpForce = 5f;
private Rigidbody rb;
private void Awake()
{
playerInput = new PlayerInput();
rb = GetComponent();
}
private void OnEnable()
{
playerInput.Gameplay.Enable();
playerInput.Gameplay.Move.performed += OnMove;
playerInput.Gameplay.Move.canceled += OnMove;
playerInput.Gameplay.Jump.performed += OnJump;
}
private void OnDisable()
{
playerInput.Gameplay.Disable();
playerInput.Gameplay.Move.performed -= OnMove;
playerInput.Gameplay.Move.canceled -= OnMove;
playerInput.Gameplay.Jump.performed -= OnJump;
}
private void OnMove(InputAction.CallbackContext context)
{
moveInput = context.ReadValue();
}
private void OnJump(InputAction.CallbackContext context)
{
isJumping = context.performed;
}
private void FixedUpdate()
{
// Handle movement
Vector3 movement = new Vector3(moveInput.x, 0f, moveInput.y) * moveSpeed;
rb.AddForce(movement);
// Handle jumping
if (isJumping)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isJumping = false;
}
}
}Tip:Â Use FixedUpdate() for physics-based movement to ensure consistent behavior regardless of frame rate.
5. New Input System Advanced Features
5.1 Input Processors
Input Processors enable you to transform input data as it travels to your game logic. You can add processors to your bindings from the editor in the Input Action Asset. Some built-in ones are:
- Normalize: Normalizes vector input
- Scale: Multiplies input values by a factor
- Invert: Inverts input values
- Deadzone: Removes small unwanted inputs
5.2 Control Schemes
Control Schemes allow you to create and manage multiple input configurations for various types of devices. To create a Control Scheme:
- Open your Input Action Asset
- Click the  button alongside “Control Schemes”
- Give your scheme a name, e.g., “Keyboard&Mouse”, “Gamepad”
- Add devices to the scheme as needed
5.3 Input System Settings
You can configure global Input System settings in Project Settings > Input System Package. Some important settings include:
- Compensate for Screen Orientation: Adjusts input direction based on screen orientation
- Update Mode: Determines when input events are processed
- Filter Noise on Current: Reduces noise in analog inputs
6. Best Practices and Tips
- Use Action Maps to organize your inputs logically
- Take advantage of Composite Bindings for complex inputs (e.g., WASD movement)
- Use Input System debugger (Window > Analysis > Input Debugger) to troubleshoot input issues
- Consider using the Input System’s built-in UI input module for seamless UI navigation
- Leverage Input Interactions to define more complex input behaviors (e.g., hold, multi-tap)
Conclusion
The new Input System gives you a powerful, flexible way to handle input within your games. Once you understand its features in relation to input actions, input processors, and control schemes, you can create much more robust and adaptable input systems for your projects. Having explored this in the new Input System, you will learn much more advanced features and possibilities for improving the input handling within your game.
Remember that, for information on the latest versions and more advanced usage of the Input System, you can always check Unity’s official documentation. Happy coding, and may all of your inputs always be responsive!