5.7 Structuring Levels with Checkpoints

Checkpoints save player progress and improve the gameplay experience.

Step 1: Create a Checkpoint Object

  1. Add a 3D or 2D object (e.g., a flag or marker).
  2. Add a Collider component and check Is Trigger.

Step 2: Create a Checkpoint Script
Attach this script to the checkpoint object:

public Vector3 checkpointPosition;

void OnTriggerEnter(Collider other) {
    if (other.CompareTag("Player")) {
        GameManager.instance.SetCheckpoint(transform.position);
        Debug.Log("Checkpoint reached!");
    }
}

Step 3: Respawn Player at Checkpoint
Update the hazard script to respawn the player at the last checkpoint:

void OnCollisionEnter(Collision collision) {
    if (collision.gameObject.CompareTag("Player")) {
        collision.gameObject.transform.position = GameManager.instance.GetCheckpoint();
    }
}