Skip to main content

🕹️ Lesson 4.2: Moving a Player Character

Time to make something you can actually control. We'll build a player from a capsule and write a short script that walks it around using the input you learned to read.

🎯 Learning Objectives

  • Set up a simple player object
  • Combine Horizontal + Vertical input into a movement direction
  • Move the player smoothly with Time.deltaTime and a tunable speed
  • Understand why we build a Vector3 movement each frame

Estimated Time: 40 minutes

In This Lesson

Building the Player

Our player will be a simple capsule (a great stand-in for a character). Here it is on the floor with a couple of collectibles nearby — the beginnings of a game:

A real Unity render of a blue capsule player on a grey floor with two yellow collectible cubes.
Figure 1: Our player (blue capsule) and some collectibles (real render). We'll drive the capsule with the keyboard.
  1. Add a Plane for the floor and a Capsule for the player.
  2. Set the Capsule's Position Y to 1 so it stands on the floor.
  3. Rename the Capsule to Player and (optionally) give it a coloured material.

The Movement Script

Create a script called PlayerMovement and attach it to the Player:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed = 5f;   // units per second

    void Update()
    {
        // 1. Read input (-1..+1 on each axis)
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        // 2. Build a direction: X from h, Z from v
        Vector3 move = new Vector3(h, 0f, v);

        // 3. Apply movement, scaled by speed and frame time
        transform.Translate(move * speed * Time.deltaTime);
    }
}

Press Play and steer with WASD or the arrow keys. Your capsule glides around the floor. That's a controllable character in twelve lines!

How It Works

graph LR A[Read h and v
from keys] --> B[Make Vector3
h, 0, v] B --> C[Multiply by speed
and deltaTime] C --> D[transform.Translate
moves the player]
  • We put h on X (left/right) and v on Z (forward/back). Y stays 0 — we don't want the player flying up.
  • Holding two keys (e.g. up + right) gives diagonal movement automatically, because both axes have values.
  • Time.deltaTime keeps the speed consistent everywhere (Lesson 3.3).

✅ Pro Tip

Tune Speed in the Inspector while the game runs. Too slow feels sluggish; too fast feels slippery. Finding the "feel" by tweaking a public field is exactly how real games are balanced.

⚠️ Diagonal is faster?

With raw axis values, moving diagonally can be slightly faster than straight. For this course that's fine; if it bothers you, look up Vector3.ClampMagnitude or .normalized later. Don't let it distract you now.

Hands-on Exercise

🏋️ Exercise: Walk the floor

  1. Build the Player capsule and attach PlayerMovement.
  2. Press Play and drive around with WASD / arrows.
  3. Change Speed in the Inspector to find a feel you like.
  4. Challenge: add a jump — when Space is pressed, nudge the player's Y up. (We'll do proper physics jumps in Module 5; a simple version is fine here.)
💡 The player won't move

Three usual causes: (1) the script isn't on the Player, (2) Speed is 0, or (3) Active Input Handling isn't set to Both (Lesson 4.1). Check the Console for input errors.

✅ Simple jump nudge
if (Input.GetKeyDown(KeyCode.Space))
{
    transform.Translate(0f, 1.5f, 0f);  // a quick hop (not real physics)
}

🎯 Quick Quiz

Question 1: In new Vector3(h, 0f, v), why is the middle value 0?

Question 2: Removing * Time.deltaTime would cause…

Summary

🎉 Key Takeaways

  • Read Horizontal and Vertical, then build a Vector3(h, 0, v).
  • Move with transform.Translate(move * speed * Time.deltaTime).
  • Keep Y at 0 to stay grounded; tune speed in the Inspector for feel.
  • Diagonal input combines automatically from the two axes.

🚀 What's Next?

Your player moves — but the camera sits still and the player can wander off screen. Lesson 4.3 makes the camera follow.

🎉 You have a playable character!