Summary
Unity’s legacy Input Manager provides a straightforward API for reading keyboard and mouse input inside Update. The two most common methods for movement are Input.GetAxisRaw and Input.GetAxis, which read named axes configured in Project Settings. For specific keys, Input.GetKey, Input.GetKeyDown, and Input.GetKeyUp take a KeyCode enum value.
Key ideas
Input.GetAxisRaw(string axisName) returns -1, 0, or 1 with no interpolation. The “Horizontal” axis maps to A/D and left/right arrow keys. “Vertical” maps to W/S and up/down arrow keys. Recommended for beginners because the result is always a clean integer-like value.
Input.GetAxis(string axisName) returns a float between -1 and 1 with built-in acceleration and deceleration smoothing. Produces more natural-feeling analogue movement but makes the value harder to reason about precisely.
Input.GetKey(KeyCode key) returns true every frame the key is held down. Use for held modifiers (sprint, aim) and for continuous actions.
Input.GetKeyDown(KeyCode key) returns true only on the single frame the key is first pressed. Use for one-shot actions: jump, attack, interact.
Input.GetKeyUp(KeyCode key) returns true only on the frame the key is released.
In practice
Reading movement axes:
void Update()
{
// Returns -1, 0, or 1. No smoothing.
float horizontal = Input.GetAxisRaw("Horizontal"); // A/D or left/right arrows
float vertical = Input.GetAxisRaw("Vertical"); // W/S or up/down arrows
transform.Translate(horizontal * moveSpeed * Time.deltaTime,
vertical * moveSpeed * Time.deltaTime, 0f);
}Sprint modifier (held key):
float currentSpeed = Input.GetKey(KeyCode.LeftShift)
? moveSpeed * sprintMultiplier
: moveSpeed;One-shot action (key press):
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Jump(); // fires once per press, not every frame
}
}Common KeyCode values:
| KeyCode | Key |
|---|---|
KeyCode.Space | Space bar |
KeyCode.LeftShift | Left Shift |
KeyCode.E | E key |
KeyCode.Escape | Escape |
KeyCode.Return | Enter |
Gotchas
Input.GetAxisRawvsInput.GetAxis: for top-down or grid-like 2D movement,GetAxisRawis almost always clearer.GetAxisis better for analogue controller feel or vehicle steering.- All
Inputcalls must happen insideUpdate(or a method called fromUpdate). Calling them inStartorFixedUpdatewill miss inputs or behave unexpectedly. - Axis names (“Horizontal”, “Vertical”) are case-sensitive and must match entries in Edit > Project Settings > Input Manager exactly.
- The Input Manager is Unity’s legacy input system. Unity 6 also includes the newer Input System package (separate API). CRE132 uses the legacy system.
Practice
Create a player script that does three things:
- moves with
Input.GetAxisRaw("Horizontal")andInput.GetAxisRaw("Vertical") - sprints while Left Shift is held
- logs
"Jump"once when Space is pressed
Success test:
- holding Space does not spam jump logs
- holding Left Shift changes movement speed every frame
- releasing movement keys returns axis values to
0
Extension:
- swap
GetAxisRawforGetAxisand describe the difference in movement feel
Self-test
- Which method returns only
-1,0or1:GetAxisorGetAxisRaw? - Which method is correct for a one-shot jump:
GetKeyorGetKeyDown? - Why should input normally be read in
Update? - What happens if the axis name is misspelled?
- Why is
GetAxisRawoften easier for beginners?
Answers
Input.GetAxisRaw.Input.GetKeyDown, because it fires once on the first pressed frame.Updateruns every rendered frame, so it is less likely to miss short input presses.- Unity cannot find the configured axis, so the input will not behave as expected.
- The value is clean and predictable: left or down is
-1, no input is0, right or up is1.
Related
- monobehaviour-lifecycle -
Updateis where input is read - unity-transform - applying movement values from input
- csharp-control-flow - using
ifwith input conditions - unity-input-system - Unity’s current package-based input workflow and main modern alternative to the legacy Input Manager