Skip to main content

๐ŸŽน Lesson 4.1: Reading Player Input

A game responds to you. In this lesson you'll learn to read the keyboard and mouse in code โ€” the first half of turning a static object into a controllable player.

๐ŸŽฏ Learning Objectives

  • Read directional input with Input.GetAxis
  • Detect key presses with Input.GetKey and Input.GetKeyDown
  • Detect mouse clicks with Input.GetMouseButtonDown
  • Fix the Unity 6 "Input not set up" gotcha

Estimated Time: 35 minutes

In This Lesson

One-time Setup (Unity 6)

This course uses Unity's classic, beginner-friendly Input class. Unity 6 sometimes ships projects set to only use the newer Input System package, which makes Input.GetAxis throw an error. Set it once and forget it:

  1. Go to Edit โ–ธ Project Settings โ–ธ Player.
  2. Under Other Settings โ–ธ Active Input Handling, choose Both (or Input Manager (Old)).
  3. Unity may ask to restart โ€” let it.

โš ๏ธ If you see "InvalidOperationException: You are trying to read Input using the UnityEngine.Input class, butโ€ฆ"

That's exactly this setting. Switch Active Input Handling to Both and restart. Then the code in this module works.

Directional Input: GetAxis

Input.GetAxis gives a smooth number from -1 to +1 for a named axis. Unity pre-defines two:

  • "Horizontal" โ€” A/D and โ† โ†’ keys. Left = -1, right = +1.
  • "Vertical" โ€” W/S and โ†‘ โ†“ keys. Down = -1, up = +1.
void Update()
{
    float h = Input.GetAxis("Horizontal");  // -1 .. +1
    float v = Input.GetAxis("Vertical");    // -1 .. +1
    Debug.Log("h: " + h + "   v: " + v);
}

Press Play and hold the arrow keys while watching the Console โ€” the numbers glide toward ยฑ1 and back to 0. In the next lesson we'll feed these straight into movement.

๐Ÿ’ก Why -1 to +1? It's a direction and strength in one number. Multiply it by your speed and you get movement that respects which key is held โ€” no separate "if left / if right" needed.

Key Presses

MethodTrue whenโ€ฆUse for
Input.GetKey(KeyCode.Space)the key is held (every frame)continuous actions (holding to charge)
Input.GetKeyDown(KeyCode.Space)the frame the key is first pressedone-shot actions (jump, shoot)
Input.GetKeyUp(KeyCode.Space)the frame the key is releasedrelease actions
void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        Debug.Log("Jump!");   // prints ONCE per press
    }
}

โš ๏ธ Down vs Key

Use GetKeyDown for actions that should happen once per press (jump, fire). Use GetKey only for things that repeat while held. Mixing them up causes "my character jumps 60 times" bugs.

Mouse Clicks

void Update()
{
    if (Input.GetMouseButtonDown(0))   // 0 = left, 1 = right, 2 = middle
    {
        Debug.Log("Left click!");
    }
}

Just like keys, there's GetMouseButton (held) and GetMouseButtonDown (this frame). The mouse's position is available as Input.mousePosition.

Hands-on Exercise

๐Ÿ‹๏ธ Exercise: Input reporter

  1. Make sure Active Input Handling is set to Both.
  2. Create a script InputTester that prints Horizontal and Vertical every frame.
  3. Add a GetKeyDown(KeyCode.Space) that logs "Jump!" once per press.
  4. Add a left-click that logs "Fire!".
  5. Attach it to any object, press Play, and exercise every input while watching the Console.
๐Ÿ’ก Errors about Input?

Set Edit โ–ธ Project Settings โ–ธ Player โ–ธ Active Input Handling to Both and restart Unity.

๐ŸŽฏ Quick Quiz

Question 1: What range of values does Input.GetAxis("Horizontal") return?

Question 2: For a jump that should happen once per press, which do you use?

Summary

๐ŸŽ‰ Key Takeaways

  • Set Active Input Handling to Both so the classic Input class works in Unity 6.
  • Input.GetAxis("Horizontal"/"Vertical") gives a smooth -1..+1 direction.
  • GetKeyDown = once per press; GetKey = every frame held.
  • GetMouseButtonDown(0) detects a left click.

๐Ÿš€ What's Next?

You can read input โ€” now let's turn it into movement. Lesson 4.2 makes a player character walk around.

๐ŸŽ‰ The game can hear you now!