The New Input System in Unity helps you read player actions like keyboard presses, mouse clicks, or gamepad buttons in a simple and flexible way.
0
0
New Input System overview in Unity
Introduction
You want to support multiple devices like keyboard, mouse, and gamepads without writing separate code for each.
You want to easily change controls or add new input methods without changing your game logic.
You want to handle complex input like combos or multiple buttons pressed together.
You want to support input rebinding so players can customize controls.
You want to improve input handling performance and reliability in your game.
Syntax
Unity
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
private PlayerInputActions inputActions;
private void Awake()
{
inputActions = new PlayerInputActions();
}
private void OnEnable()
{
inputActions.Enable();
}
private void OnDisable()
{
inputActions.Disable();
}
private void Update()
{
Vector2 move = inputActions.Player.Move.ReadValue<Vector2>();
// Use move vector to move player
}
}You create an Input Actions asset in Unity to define controls, then generate a C# class to access them.
Enable input actions when the object is active, and disable them when not to save resources.
Examples
This listens for the Jump action and calls the Jump() method when the button is pressed.
Unity
inputActions.Player.Jump.performed += ctx => Jump();
This reads the current value of the Move action, usually from a joystick or WASD keys.
Unity
Vector2 moveInput = inputActions.Player.Move.ReadValue<Vector2>();
Enable and disable input actions to control when input is processed.
Unity
inputActions.Player.Enable(); // Later inputActions.Player.Disable();
Sample Program
This simple program shows how to read movement input and listen for a jump button press using the New Input System.
Unity
using UnityEngine; using UnityEngine.InputSystem; public class SimplePlayerController : MonoBehaviour { private PlayerInputActions inputActions; private void Awake() { inputActions = new PlayerInputActions(); } private void OnJump(InputAction.CallbackContext context) { Debug.Log("Jump pressed!"); } private void OnEnable() { inputActions.Enable(); inputActions.Player.Jump.performed += OnJump; } private void OnDisable() { inputActions.Player.Jump.performed -= OnJump; inputActions.Disable(); } private void Update() { Vector2 move = inputActions.Player.Move.ReadValue<Vector2>(); Debug.Log($"Moving: {move}"); } }
OutputSuccess
Important Notes
Remember to generate the C# class from your Input Actions asset before using it in code.
Use the Input Debugger window in Unity to see what inputs are detected during play.
Summary
The New Input System lets you handle many input devices easily.
You define actions in an asset and use generated code to read inputs.
Enable and disable input actions to control when input is active.