๐น 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.GetKeyandInput.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:
- Go to Edit โธ Project Settings โธ Player.
- Under Other Settings โธ Active Input Handling, choose Both (or Input Manager (Old)).
- 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
| Method | True 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 pressed | one-shot actions (jump, shoot) |
Input.GetKeyUp(KeyCode.Space) | the frame the key is released | release 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
- Make sure Active Input Handling is set to Both.
- Create a script
InputTesterthat prints Horizontal and Vertical every frame. - Add a
GetKeyDown(KeyCode.Space)that logs "Jump!" once per press. - Add a left-click that logs "Fire!".
- 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
Inputclass 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.