Lesson 23 +10 XP

Legacy Input Manager

Legacy Input Manager

Unity's legacy input system, managed by the Input class, relies on polling keys and pre-configured virtual axes.

Polling Keys and Buttons

To check if a specific key is pressed:

  • Input.GetKey(KeyCode.Space): Returns true continuously while the spacebar is held down.
  • Input.GetKeyDown(KeyCode.Space): Returns true only on the single frame the user presses the spacebar.
  • Input.GetKeyUp(KeyCode.Space): Returns true on the single frame the user releases the spacebar.

Virtual Axes

Instead of hardcoding specific keys (like W/A/S/D), you use Virtual Axes configured under Edit -> Project Settings -> Input Manager:

  • Input.GetAxis("Horizontal"): Returns a value between -1.0 and 1.0 (smoothly interpolated, useful for keyboards or joystick analog sticks).
  • Input.GetAxisRaw("Horizontal"): Returns exactly -1, 0, or 1 with no smoothing (useful for snappy 2D movements).
void Update()
{
    // Read keyboard or controller axes
    float moveX = Input.GetAxis("Horizontal");
    float moveZ = Input.GetAxis("Vertical");
    Vector3 movement = new Vector3(moveX, 0, moveZ);
    transform.Translate(movement * speed * Time.deltaTime);
}

Mouse Input

  • Input.mousePosition: Returns the current mouse position in screen pixel coordinates.
  • Input.GetMouseButtonDown(0): Returns true when the left mouse button (0) is clicked. (1 is right click, 2 is middle click).

TL;DR

  • The legacy Input class polls inputs every frame inside Update.
  • GetKeyDown triggers once; GetKey triggers continuously.
  • Use virtual axes like Input.GetAxis to support both keyboards and gamepads.
  • Read mouse clicks using GetMouseButtonDown.