Handling Input

Unity has two ways to read the keyboard, mouse and gamepad: the older Input Manager (the Input class) and the newer Input System package. This page helps you pick one and get started.

Which one am I using?

The old Input Manager is a light switch wired into the wall: simple, but if you want another switch for the same light you have to rewire it yourself. The new Input System is a universal remote. You define buttons like "Jump" once, then decide which keys or gamepad buttons send that signal. Your code only listens for "Jump" and doesn't care where it came from.

Open Edit > Project Settings > Player > Other Settings > Active Input Handling.

SettingWhat works
Input Manager (Old)Only Input.GetKey etc.
Input System Package (New)Only UnityEngine.InputSystem. Calling Input.GetKey throws an InvalidOperationException.
BothBoth work. Handy while learning.
Unity 6 projectsNew projects made from the Unity 6 templates usually have the Input System package installed and active. Most older tutorials use the old Input class. If you get the InvalidOperationException from a tutorial's code, set this option to Both.

Quick comparison

TaskOld Input ManagerNew Input System
Space pressed this frameInput.GetKeyDown(KeyCode.Space)Keyboard.current.spaceKey.wasPressedThisFrame
W held downInput.GetKey(KeyCode.W)Keyboard.current.wKey.isPressed
Left click this frameInput.GetMouseButtonDown(0)Mouse.current.leftButton.wasPressedThisFrame
Mouse position (pixels)Input.mousePositionMouse.current.position.ReadValue()
Move stick / WASDInput.GetAxis("Horizontal"), "Vertical"An Input Action, moveAction.ReadValue<Vector2>()

Option A: Old Input Manager

Simple and used in most tutorials. See the Input page for all methods.

using UnityEngine;

public class OldInputMover : MonoBehaviour
{
    [SerializeField] private float speed = 5f;

    void Update()
    {
        // "Horizontal" = A/D, Left/Right arrows, gamepad left stick X.
        // Defined in Project Settings > Input Manager.
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        transform.Translate(new Vector3(x, 0f, z) * speed * Time.deltaTime);

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("Jump!");
        }
    }
}

Option B: New Input System, reading devices directly

Quickest way to try the new system. Good for prototypes and debug keys.

using UnityEngine;
using UnityEngine.InputSystem;

public class DirectInputExample : MonoBehaviour
{
    void Update()
    {
        // .current is null when no such device is connected, so check it.
        if (Keyboard.current != null && Keyboard.current.spaceKey.wasPressedThisFrame)
        {
            Debug.Log("Jump!");
        }

        if (Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame)
        {
            Debug.Log("Clicked at " + Mouse.current.position.ReadValue());
        }
    }
}

Option C: New Input System with Actions (recommended for real games)

Instead of hard-coding keys, you define actions like "Move" and "Jump" and bind keys and gamepad buttons to them. Players can then use any device, and you can support rebinding.

Unity 6 projects include a default actions asset (InputSystem_Actions) with actions such as "Move", "Look", "Jump" and "Attack" already set up, registered in Project Settings > Input System Package.

using UnityEngine;
using UnityEngine.InputSystem;

public class ActionMover : MonoBehaviour
{
    [SerializeField] private float speed = 5f;

    private InputAction moveAction;
    private InputAction jumpAction;

    void Start()
    {
        // Look up actions from the project-wide actions asset by name.
        moveAction = InputSystem.actions.FindAction("Move");
        jumpAction = InputSystem.actions.FindAction("Jump");
    }

    void Update()
    {
        Vector2 move = moveAction.ReadValue<Vector2>(); // WASD, arrows or left stick
        transform.Translate(new Vector3(move.x, 0f, move.y) * speed * Time.deltaTime);

        if (jumpAction.WasPressedThisFrame())
        {
            Debug.Log("Jump!");
        }
    }
}

What happens at runtime

  1. At the start of each frame, before Update, Unity collects all input events from the operating system.
  2. It updates the state of every key, button and axis.
  3. "Pressed this frame" / GetKeyDown values are true for exactly one frame: the frame the press happened. "Is pressed" / GetKey stays true every frame while held.
  4. Your Update code reads that state.

Common Mistakes

See also