Menu
CoddyTech

Checkpoints

Part of the Building Real Games section of Coddy's Unity journey. Lesson 50 of 56.

challenge icon

Challenge

Medium

A fall into the void should cost a little, not the whole run. Keep everything from the earlier lessons. Two red poles stand on the course, Checkpoint1 and Checkpoint2, each with a trigger and the Checkpoint script.

In Player:

  • A field fallY that the Inspector sets (-5), and a respawn point that starts where the player starts.
  • A public method SetCheckpoint(Vector3 point) that makes point the respawn point.
  • When the player drops below fallY, put it back at the respawn point and clear its velocity. Setting transform.position is not enough on its own: the controller's next Move starts from where the physics last saw it and undoes the move. Switch the controller off (controller.enabled = false) while you move the player, and on again after.

In Checkpoint:

  • The first time the player (an object with the Player script) enters the trigger, give the player the checkpoint's position as its respawn point, turn the pole green (Color.green) and log Checkpoint reached.
  • Entering it again does nothing, so an old checkpoint never moves the respawn point back.

Press Play, reach a checkpoint and jump into the void, then Check.

Try it yourself

using UnityEngine;

public class Checkpoint : MonoBehaviour
{
    // A later lesson fills this in: a flag that moves where you come back after a fall.
}

All lessons in Building Real Games