Lesson Overview
In Lesson 5, you created your first C# script and displayed a message in Unity's Console. This lesson makes your GameObject interactive. You will learn how keyboard input, variables, coordinates, and the Update() method work together to create real-time movement.
1. Learning Objectives
- Create a 2D player sprite in Unity.
- Write and attach a C# player movement script.
- Read WASD and arrow-key input.
- Distinguish
Start()fromUpdate(). - Use a variable to control movement speed.
- Use
Time.deltaTimefor frame-rate-independent movement. - Test, debug, and explain the result.
2. Key Vocabulary
| Term | Definition |
|---|---|
| Keyboard Input | Information received when the user presses a keyboard key. |
| Variable | A named location that stores a value such as speed. |
Update() | A Unity method called each frame while a script is active. |
float | A data type for decimal numbers. |
Vector3 | A value holding X, Y, and Z coordinates. |
transform.position | A GameObject's position in a scene. |
Time.deltaTime | Time elapsed between frames, in seconds. |
| Input System | Unity's modern package for keyboard, mouse, and controller input. |
3. Warm-Up — 5 Minutes
Discussion: How does a game character know which direction to move when the player presses a key?
- D or →: Increase X (right).
- A or ←: Decrease X (left).
- W or ↑: Increase Y (up).
- S or ↓: Decrease Y (down).
4. Guided Activity — 25 Minutes
Step 1 — Open Unity
- Open your existing Unity 2D project.
- Create a new scene or open your previous scene.
- Save it as
Lesson6_PlayerMovement.
Step 2 — Create the player sprite
- In the Hierarchy, right-click and choose 2D Object → Sprites → Square.
- Rename it
Player. - Set Transform Position to X = 0, Y = 0, Z = 0.
- Choose a color, then confirm the Main Camera can see the sprite.
Step 3 — Prepare the Input System
Open Window → Package Manager and install Input System if needed. In Edit → Project Settings → Player → Other Settings, set Active Input Handling to Input System Package (New) or Both. The editor may ask to restart.
Input.GetKey() API.Step 4 — Create a script
- Open the
Scriptsfolder in the Project window. - Create a MonoBehaviour script named
PlayerMovement. - Open it in your C# editor and replace its contents with the code below.
- Save your code.
5. Complete C# Program
using UnityEngine;
using UnityEngine.InputSystem;
// Lesson 6: Move the Player
// Move a 2D sprite with WASD or arrow keys.
public class PlayerMovement : MonoBehaviour
{
// Speed in Unity units per second.
public float moveSpeed = 5f;
// Runs once when the script starts.
void Start()
{
Debug.Log("Player movement is ready!");
}
// Runs once every frame.
void Update()
{
// Stop if no keyboard is available.
if (Keyboard.current == null)
{
return;
}
// Store directional input.
float moveX = 0f;
float moveY = 0f;
// Left
if (Keyboard.current.aKey.isPressed ||
Keyboard.current.leftArrowKey.isPressed)
{
moveX -= 1f;
}
// Right
if (Keyboard.current.dKey.isPressed ||
Keyboard.current.rightArrowKey.isPressed)
{
moveX += 1f;
}
// Up
if (Keyboard.current.wKey.isPressed ||
Keyboard.current.upArrowKey.isPressed)
{
moveY += 1f;
}
// Down
if (Keyboard.current.sKey.isPressed ||
Keyboard.current.downArrowKey.isPressed)
{
moveY -= 1f;
}
// Combine horizontal and vertical motion.
Vector3 movement = new Vector3(moveX, moveY, 0f);
// Do not move faster diagonally.
movement = movement.normalized;
// Move at a consistent speed across frame rates.
transform.position +=
movement * moveSpeed * Time.deltaTime;
}
}Step 5 — Attach the script
- Return to Unity and wait for compilation to finish.
- Select
Playerin the Hierarchy. - Drag
PlayerMovementonto its Inspector or use Add Component. - In the Inspector, set Move Speed to
5.
Step 6 — Run your program
- Click Play, then click inside the Game view to focus it.
- Press W/A/S/D or the arrow keys to move your character.
- Release the keys and verify the character stops.
- Press two direction keys together to test diagonal movement.
6. Interactive Movement Demonstration
Use the buttons below, or click the demo area and use WASD/arrow keys. This browser demo illustrates coordinates; the C# code above is what students run in Unity.
X: 0 | Y: 0
| Key | Expected movement |
|---|---|
| W or ↑ | Up |
| S or ↓ | Down |
| A or ← | Left |
| D or → | Right |
| W + D | Diagonal up-right |
| No key | Stop |
7. Understanding the Program
Variables
public float moveSpeed = 5f;public makes the speed field editable in Unity's Inspector; float holds decimal values; and 5f is the initial speed.
Keyboard input
if (Keyboard.current.dKey.isPressed)
{
moveX += 1f;
}If the D key is pressed, the script increases the X direction.
The Update method
void Update()
{
// Called every frame.
}Unlike Start(), Update() repeatedly reads input so movement responds continuously.
Time.deltaTime
transform.position +=
movement * moveSpeed * Time.deltaTime;This scales the position change by elapsed time. At a speed of five units per second, an unobstructed character travels approximately five Unity units in one second.
Diagonal speed
movement = movement.normalized;This prevents diagonal input from making the character move faster than straight-line input.
8. Independent Student Assignment
Mission: Create and customize your own player controller. Track your work using the checklist below.
0 / 10 complete
9. Challenge Activities
- Speed Experiment: Test move speeds of 2, 5, and 10; explain the difference.
- Speed Boost: Hold Shift to move twice as fast (requires an additional keyboard check and speed multiplier).
- Obstacle Course: Add five visual obstacles and navigate around them. Collisions are not implemented yet, so sprites can overlap.
- Stay on Screen: Research
Mathf.Clamp()to keep the player inside the play area.
10. Troubleshooting
| Problem | Possible solution |
|---|---|
| Player does not move | Check the component is attached and the Game view has keyboard focus. |
InputSystem not found | Install/enable the Input System package. |
| Player moves too quickly | Lower moveSpeed in the Inspector. |
| Player disappears | Check camera placement, player Z coordinate, and movement boundaries. |
| Compilation error | Verify brackets, semicolons, spelling, and matching class/filename. |
| Fast diagonal movement | Verify movement.normalized is present. |
11. Knowledge Check — Interactive Quiz
12. Assessment Rubric — 100 Points
| Criteria | Points |
|---|---|
| Player sprite created correctly | 10 |
| C# script created and attached | 15 |
| Keyboard movement in four directions | 25 |
Correct use of Update() | 10 |
| Movement speed variable | 10 |
Time.deltaTime and diagonal movement control | 10 |
| Five meaningful comments | 10 |
| Successful execution and evidence | 10 |
| Total | 100 |
13. Submission Instructions
Submit the completed PlayerMovement.cs file and a screenshot showing the Player in the scene with its movement component visible in the Inspector. Demonstrate movement in all directions. Save the scene and project files according to your teacher's directions.
14. Exit Ticket
- Why is
Update()better thanStart()for reading continuous movement input? - What changes when you increase
moveSpeed? - Why multiply movement by
Time.deltaTime? - What happens when no movement keys are pressed?
Teacher Notes
This lesson builds on Lesson 5's introduction to C# and MonoBehaviour. Suggested pacing: 5 minutes warm-up, 25 minutes guided instruction, 15 minutes independent practice, and 5–10 minutes for review and troubleshooting.
Mathf.Clamp().Reference: Unity Input System documentation · MonoBehaviour.Update documentation