Checkpoints
Part of the Building Real Games section of Coddy's Unity journey. Lesson 50 of 56.
Challenge
MediumA 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
fallYthat the Inspector sets (-5), and a respawn point that starts where the player starts. - A public method
SetCheckpoint(Vector3 point)that makespointthe respawn point. - When the player drops below
fallY, put it back at the respawn point and clear itsvelocity. Settingtransform.positionis not enough on its own: the controller's nextMovestarts 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
Playerscript) enters the trigger, give the player the checkpoint's position as its respawn point, turn the pole green (Color.green) and logCheckpoint 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
2Buttons and Screens
Buttons and onClickLambdasWiring in the InspectorPanels On and OffHealth BarsRecap: Upgrade Shop5Saving with PlayerPrefs
Save a NumberLoad with a DefaultHigh ScoreSave SettingsRecap: Coins in the Bank3The Game Manager
One Script Runs the GameThe SingletonGame StatesSwitching on the StateRecap: Round Manager6Project: Endless Runner
Three LanesObstacles Ahead9The New Input System
Two Input SystemsKeyboard and MousePressed This FrameInput ActionsRecap: Input Swap