Input
| Type | Class (static members) |
|---|---|
| Namespace | UnityEngine |
| System | Old Input Manager |
| Newer alternative | Input 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:
| Suffix | True when | Use for |
|---|---|---|
...Down | Only on the one frame the key was pressed | Jump, shoot once, open menu |
| (none) | Every frame while held | Sprint, automatic fire, charging |
...Up | Only on the frame it was released | Release a charged attack |
Static Methods
| Method | Returns | Description |
|---|---|---|
GetKey(KeyCode key) | bool | Is the key held? e.g. KeyCode.W, KeyCode.LeftShift. |
GetKeyDown(KeyCode key) | bool | Pressed this frame. |
GetKeyUp(KeyCode key) | bool | Released this frame. |
GetAxis(string axisName) | float | -1 to 1, smoothed. "Horizontal", "Vertical", "Mouse X", "Mouse Y". |
GetAxisRaw(string axisName) | float | Same but not smoothed: -1, 0 or 1 for keys. Snappier. |
GetButton(string buttonName) | bool | Named buttons like "Jump" and "Fire1" (configurable). Also GetButtonDown/Up. |
GetMouseButton(int button) | bool | 0 = left, 1 = right, 2 = middle. Also Down/Up versions. |
Static Properties
| Name | Type | Description |
|---|---|---|
mousePosition | Vector3 | Mouse position in pixels. (0,0) is the bottom-left of the screen. |
mouseScrollDelta | Vector2 | Scroll wheel movement this frame (use .y). |
anyKeyDown | bool | Any key or mouse button pressed this frame. "Press any key to start". |
touchCount | int | Number 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
- At the start of each frame, Unity reads the OS input events and updates key states.
GetKeyDownis true for that single frame only. If you check it inFixedUpdateand no physics step runs that frame, you never see it.GetAxisdoesn'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
- Checking
...Downin FixedUpdate. Presses get missed. Check inUpdate. - Typos in axis names.
GetAxis("horizontal")(lowercase) throws "Input Axis horizontal is not setup". Names are case-sensitive. - Hard-coding keys everywhere. Fine for learning; for real games, the Input System makes rebinding and gamepad support much easier.
See also
- Handling Input
- InputAction (new Input System)
- Tutorial: Using the Input System
This page is a simplified, AI-assisted interpretation. For the complete and authoritative reference, see the official Unity documentation.