EasyBeginner C#50–60 minutesHigh School

Unity Game Engine
Lesson 6: Move the Player

Mission: Take Control of Your Character! Program a 2D sprite to move left, right, up, and down using WASD or arrow keys.

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

  1. Create a 2D player sprite in Unity.
  2. Write and attach a C# player movement script.
  3. Read WASD and arrow-key input.
  4. Distinguish Start() from Update().
  5. Use a variable to control movement speed.
  6. Use Time.deltaTime for frame-rate-independent movement.
  7. 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).
W / ↑ S / ↓ A / ← D / → X Y

4. Guided Activity — 25 Minutes

Step 1 — Open Unity

  1. Open your existing Unity 2D project.
  2. Create a new scene or open your previous scene.
  3. Save it as Lesson6_PlayerMovement.

Step 2 — Create the player sprite

  1. In the Hierarchy, right-click and choose 2D Object → Sprites → Square.
  2. Rename it Player.
  3. Set Transform Position to X = 0, Y = 0, Z = 0.
  4. 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.

This lesson uses Unity's newer Input System, not the older Input.GetKey() API.

Step 4 — Create a script

  1. Open the Scripts folder in the Project window.
  2. Create a MonoBehaviour script named PlayerMovement.
  3. Open it in your C# editor and replace its contents with the code below.
  4. 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

  1. Return to Unity and wait for compilation to finish.
  2. Select Player in the Hierarchy.
  3. Drag PlayerMovement onto its Inspector or use Add Component.
  4. In the Inspector, set Move Speed to 5.

Step 6 — Run your program

  1. Click Play, then click inside the Game view to focus it.
  2. Press W/A/S/D or the arrow keys to move your character.
  3. Release the keys and verify the character stops.
  4. 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

  1. Speed Experiment: Test move speeds of 2, 5, and 10; explain the difference.
  2. Speed Boost: Hold Shift to move twice as fast (requires an additional keyboard check and speed multiplier).
  3. Obstacle Course: Add five visual obstacles and navigate around them. Collisions are not implemented yet, so sprites can overlap.
  4. 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

  1. Why is Update() better than Start() for reading continuous movement input?
  2. What changes when you increase moveSpeed?
  3. Why multiply movement by Time.deltaTime?
  4. 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.

Next Lesson — Lesson 7: Keep the Player Inside the Game Area. Introduce coordinate boundaries, conditions, and Mathf.Clamp().

Reference: Unity Input System documentation · MonoBehaviour.Update documentation