Checkpoints save player progress and improve the gameplay experience.
Step 1: Create a Checkpoint Object
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();
}
}