Overview
Input is the most important interface connecting players to games. Detecting keyboard presses, mouse clicks, and gamepad stick movements to move characters and execute actions is fundamental. Unity provides systems for easily handling these inputs.
This article focuses on input handling using Unity's traditional Input Manager, explaining how to receive input from keyboards, mice, and gamepads. We'll also touch on the newer Input System package for more advanced and flexible input management.
Input Manager Basics
Unity's standard input system, Input Manager, is configured via Edit > Project Settings > Input Manager. Here you'll find predefined "Virtual Axes" like Horizontal, Vertical, Jump, and Fire1.
For example, the Horizontal axis maps to keyboard keys a and d, plus left/right arrow keys. This means Input.GetAxis("Horizontal") responds to any of these keys. This abstraction makes it easier to create settings screens where players can customize key bindings.
Getting Keyboard Input
Keyboard input is retrieved using various methods from the Input class.
| Method | Description |
|---|---|
Input.GetKeyDown(KeyCode) | Returns true the moment the key is pressed. Ideal for single actions like jumping or shooting. |
Input.GetKey(KeyCode) | Returns true while the key is held down. Use for continuous actions like accelerating movement. |
Input.GetKeyUp(KeyCode) | Returns true the moment the key is released. Use for charged shot releases, etc. |
KeyCode is an enum defining each keyboard key. (e.g., KeyCode.Space, KeyCode.W, KeyCode.Return)
using UnityEngine;
public class KeyboardInputExample : MonoBehaviour
{
void Update()
{
// Jump when Space is pressed
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Jump!");
}
// Reloading while R is held
if (Input.GetKey(KeyCode.R))
{
Debug.Log("Reloading...");
}
// Close pause menu when Escape is released
if (Input.GetKeyUp(KeyCode.Escape))
{
Debug.Log("Closing pause menu");
}
}
}
Getting Input via Virtual Axes
For more flexible input not tied to specific keys, use virtual axes.
Input.GetAxis(string axisName): Returns the current value of the specified axis. Values smoothly transition between -1.0 and 1.0. Keyboard input snaps to -1, 0, or 1, while gamepad sticks return intermediate values based on tilt.Input.GetAxisRaw(string axisName): Similar toGetAxis, but returns -1, 0, or 1 immediately without smoothing. Better for snappy, responsive movement.
using UnityEngine;
public class AxisInputExample : MonoBehaviour
{
public float speed = 10f;
void Update()
{
// Get "Horizontal" axis value (A/D keys, left/right arrows)
float horizontalInput = Input.GetAxis("Horizontal");
// Move left/right using the input value
transform.Translate(Vector3.right * horizontalInput * speed * Time.deltaTime);
}
}
Getting Mouse Input
Mouse clicks and position are also retrieved via the Input class.
Input.GetMouseButtonDown(int button): Mouse button pressed.0= left click,1= right click,2= middle click.Input.GetMouseButton(int button): Mouse button held down.Input.GetMouseButtonUp(int button): Mouse button released.Input.mousePosition: Returns mouse cursor screen coordinates (in pixels) asVector3. Z is always 0.
using UnityEngine;
public class MouseInputExample : MonoBehaviour
{
void Update()
{
// On left click
if (Input.GetMouseButtonDown(0))
{
// Get mouse screen coordinates
Vector3 mousePos = Input.mousePosition;
Debug.Log("Left click! Position: " + mousePos);
// Can cast a ray from screen coordinates to click 3D objects
}
}
}
About the New Input System
The Input class approach shown above is intuitive and easy to understand, but has some drawbacks:
- Input logic tends to scatter throughout
Updatefunctions. - Managing complex input across multiple devices (keyboard, gamepad, touch) is difficult.
- Implementing runtime key rebinding is challenging.
To solve these issues, Unity developed the Input System package, installed via Package Manager. The Input System uses an event-based approach, managing input in a more structured way with PlayerInput components and InputAction assets.
While it has a steeper learning curve for beginners, for serious game development—especially multi-platform projects or those requiring complex input configurations—the Input System is well worth considering.
Summary
Input handling is the crucial link between players and the game world.
Input.GetKeyDown: Detect key press moment. For single actions.Input.GetKey: Detect key held. For continuous actions.Input.GetAxis: Get virtual axis input. Works well with both keyboards and gamepads.Input.GetMouseButtonDown: Detect mouse clicks.
Start by getting comfortable with the standard Input Manager for basic character movement and actions. As your project grows more complex, consider migrating to the new Input System as a great next step.