Input
TypeClass (static members)
NamespaceUnityEngine
SystemOld Input Manager
Newer alternativeInput System

Input

The Input class reads the keyboard, mouse and simple gamepad input using the old Input Manager. It's simple and used in most tutorials. For new projects, also look at the Input System.

Requires the old system to be enabledIf Project Settings > Player > Active Input Handling is set to "Input System Package (New)", every call on this page throws an InvalidOperationException. Set it to "Both" to use this class. See Handling Input.

Down, held, up

Most methods come in three flavors:

SuffixTrue whenUse for
...DownOnly on the one frame the key was pressedJump, shoot once, open menu
(none)Every frame while heldSprint, automatic fire, charging
...UpOnly on the frame it was releasedRelease a charged attack

Static Methods

MethodReturnsDescription
GetKey(KeyCode key)boolIs the key held? e.g. KeyCode.W, KeyCode.LeftShift.
GetKeyDown(KeyCode key)boolPressed this frame.
GetKeyUp(KeyCode key)boolReleased this frame.
GetAxis(string axisName)float-1 to 1, smoothed. "Horizontal", "Vertical", "Mouse X", "Mouse Y".
GetAxisRaw(string axisName)floatSame but not smoothed: -1, 0 or 1 for keys. Snappier.
GetButton(string buttonName)boolNamed buttons like "Jump" and "Fire1" (configurable). Also GetButtonDown/Up.
GetMouseButton(int button)bool0 = left, 1 = right, 2 = middle. Also Down/Up versions.

Static Properties

NameTypeDescription
mousePositionVector3Mouse position in pixels. (0,0) is the bottom-left of the screen.
mouseScrollDeltaVector2Scroll wheel movement this frame (use .y).
anyKeyDownboolAny key or mouse button pressed this frame. "Press any key to start".
touchCountintNumber of fingers on a touchscreen. Use GetTouch(i) for details.

Code Samples

Top-down movement

using UnityEngine;

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

    void Update()
    {
        float x = Input.GetAxisRaw("Horizontal");
        float z = Input.GetAxisRaw("Vertical");
        Vector3 dir = new Vector3(x, 0f, z).normalized; // normalized: diagonals aren't faster
        transform.position += dir * speed * Time.deltaTime;
    }
}

Charge-and-release attack

private float chargeStart;

void Update()
{
    if (Input.GetMouseButtonDown(0)) chargeStart = Time.time;

    if (Input.GetMouseButtonUp(0))
    {
        float charge = Mathf.Clamp01((Time.time - chargeStart) / 2f); // full after 2 s
        Debug.Log("Released with " + (charge * 100f) + "% power");
    }
}

What happens at runtime

  1. At the start of each frame, Unity reads the OS input events and updates key states.
  2. GetKeyDown is true for that single frame only. If you check it in FixedUpdate and no physics step runs that frame, you never see it.
  3. GetAxis doesn't jump straight to 1. It moves toward it at the "Sensitivity" speed set in Project Settings > Input Manager, and back toward 0 at the "Gravity" speed.

Common Mistakes

See also