Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 36 ยท UI and Input

The Unity Input System

Move from the old Input Manager to Unity's Input System package - actions, bindings, action maps, PlayerInput, gamepad and touch - with runnable input-mapping stand-ins.

Players use keyboards, mice, gamepads, touch screens and sometimes all four at once. Reading raw keys everywhere in your code (if (Input.GetKey(KeyCode.W))) works for a prototype, but it does not scale: you cannot rebind keys, add a gamepad or support two players without editing every script.

Unity's Input System package solves this with actions. An action is a named intent such as "Jump" or "Move". You bind physical controls to it, and your code only ever talks about the action. This lesson explains the vocabulary, three ways to read input, and how the mapping works, with a runnable stand-in for the binding layer.

Old versus new

Unity has two systems. The Input Manager (UnityEngine.Input) is the legacy one with a fixed list of axes. The Input System package (com.unity.inputsystem) is the modern one and is the default in new Unity 6 projects. Under Edit > Project Settings > Player > Active Input Handling you choose Input Manager, Input System Package or Both. Do not mix the two APIs by accident: when only the new system is active, Input.GetKey throws an exception.

The vocabulary

  • Input Actions asset: a file (.inputactions) that holds everything. Create one with Assets > Create > Input Actions, or use the default InputSystem_Actions asset in new projects.
  • Action Map: a group of actions for one context, for example Player and UI. You enable and disable whole maps when the game switches between gameplay and a menu.
  • Action: an intent with a type: Value (continuous, like a stick), Button (pressed or not) or Pass Through (raw events).
  • Binding: a link from a control path such as <Keyboard>/space or <Gamepad>/buttonSouth to the action. A composite binding builds one value from several controls, such as WASD into a Vector2.
  • Control scheme: a named set of devices (Keyboard & Mouse, Gamepad, Touch) used to separate bindings per device type.
flowchart LR
  K["Keyboard W A S D"] --> M["Move action<br/>Vector2"]
  G["Gamepad left stick"] --> M
  T["Touch joystick"] --> M
  M --> P["PlayerController script"]

Several devices feed one action; the script never knows which device was used.

Reading input: three styles

1. Poll the action each frame. Simple and good for movement:

C#
using UnityEngine;
using UnityEngine.InputSystem;

public class Mover : MonoBehaviour
{
    [SerializeField] InputActionReference move;   // drag the Move action here
    [SerializeField] float speed = 5f;

    void OnEnable()  => move.action.Enable();
    void OnDisable() => move.action.Disable();

    void Update()
    {
        Vector2 dir = move.action.ReadValue<Vector2>();
        transform.Translate(new Vector3(dir.x, 0, dir.y) * (speed * Time.deltaTime));
    }
}

2. Subscribe to callbacks. Best for one-shot buttons. An action goes through phases (Started, Performed, Canceled):

C#
void OnEnable()
{
    jump.action.performed += OnJump;
    jump.action.Enable();
}
void OnDisable()
{
    jump.action.performed -= OnJump;
    jump.action.Disable();
}
void OnJump(InputAction.CallbackContext ctx) => rb.AddForce(Vector3.up * 6f, ForceMode.Impulse);

3. Use PlayerInput. Add the PlayerInput component, assign the asset and choose a Behavior. With Send Messages it calls methods named after actions: OnMove(InputValue v), OnJump(). It also handles joining players and device switching, which makes local multiplayer much easier.

There is also direct device access (Keyboard.current.spaceKey.wasPressedThisFrame, Gamepad.current, Mouse.current.position.ReadValue()), handy for quick tests and debug keys.

How bindings become values

Let us build a tiny version of the idea. A composite 2D binding maps four buttons to a vector and normalizes it, so diagonals are not faster than straight lines. A deadzone ignores tiny stick drift.

C# Stand-in: composite binding and deadzone

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The Input System ships processors for exactly these jobs (Normalize Vector2, Stick Deadzone, Invert, Scale). You add them to a binding or an action in the editor; no script is needed.

Action maps and rebinding

Switching between gameplay and a pause menu is just swapping maps, so the "Jump" button cannot fire while a menu is open. The stand-in below models a map switch and a rebind.

C# Stand-in: action maps and rebinding

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

In Unity, InputAction.PerformInteractiveRebinding() listens for the next key press and writes the new path, and SaveBindingOverridesAsJson() serializes the result so you can store it in a settings file.

Common mistakes

  • Forgetting Enable() on an action or map: it reads zero forever.
  • Subscribing in Start and never unsubscribing; use OnEnable/OnDisable.
  • Reading input in FixedUpdate for one-shot buttons: a press can be missed or counted twice. Store the press in Update and consume it in FixedUpdate.
  • Multiplying a mouse delta by Time.deltaTime (it is already per-frame).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is an Action Map?

    Maps let you enable the controls for gameplay or menus separately.
  2. 2. Why normalize the Vector2 from a WASD composite?

    Without normalization, W+D gives a vector of length about 1.41.
  3. 3. An action never reports input in your script. Which is the most likely fix?

    Actions are disabled by default when used from code; PlayerInput enables them for you.
  4. 4. Which callback phase fires when a button press completes its interaction?

    An action goes Started, then Performed (or Canceled); the performed callback is the normal place to react.

Summary

  • The Input System maps devices to actions so code depends on intent, not keys.
  • Assets hold action maps, actions, bindings and control schemes; composites and processors shape values.
  • Read input by polling, by callbacks, or with PlayerInput; always Enable and unsubscribe correctly.
  • Switching action maps is how gameplay and menus avoid fighting over the same keys.
  • Runtime rebinding is built in and can be saved as JSON.

Next: UI Toolkit