Day 15: Creating a Checkpoint and Respawn System
Objective: To set up a system where lives are taken from the player when they fall off the stage and to have them respawn at the last checkpoint reached.
Now that we have a good way of following the player around the level and we have obstacles in the form of moving platforms, we now need to set up a system that will damage the player and reload the scene. We also need to set checkpoints around the level for the player to activate. This will give the player a sense of progress as they wont have to restart from the beginning of the level. If they run out of lives however then the entire level will restart.
First we need to create a Dead Zone GameObject at the bottom of the level. We’ll use this object with a Collider/trigger to detect when the player falls off the stage. We’ll now write the script that will handle the management of checkpoints and respawning the player.
In the DeadZone script we need hooks to the current respawn point, a bool to tell the dead zone that we are respawning, and a hook to the UI manager just for debugging purposes. The UI manager hook is not needed for this to work but I like to receive my messages without having to glance at the console.
We use an OnTriggerEnter method to detect the player. If the player is found we will subtract a life and we will set the position of the player to equal that of the current respawn point.
Respawning needs to rely on a coroutine to handle the character controller component. When have to handle the turning on and off of the character controller to successfully move the player back to the respawn position. We also want a public method that we can use to change the current spawn point whenever the player runs into a new one(or even an old one depending on your game). This method will be called once the player enters the Checkpoint objects trigger.
The check point is the parent to two 3d gameobjects that we use to represent when a checkpoint is activated. When the player runs into the check point then we will switch models from a short purple rod to a tall green rod. This is just a visual effect that will stand in for when we import art and audio. The checkpoint script will look like this.
In this script we need references to those models mentioned up above and we need a hook into the Deadzone object.
When a player enters the trigger of the checkpoint object a lot of things will happen. First we do a null check to see if we have a hook to the deadzone. If we do then we will call the public method SetSpawnPoint() to assign this current checkpoint to the deadzones active spawnpoint. Then were going to search for all objects that have the Checkpoint component so we can make it so they are all turned off while the current one is active. That means all other checkpoints will revert back to the off state and they will be short purple rods.
And that’s about it. When the player runs out of lives the game restarts and the player is repositioned to the beginning of the level.
This is the player script and we will call this public method in the deadzone script to subtract a life from the player. If the player is out of lives then we will reload the scene.
Eventually we can add features like a camera fade and sound effects to make this transition more attractive. Again, I set up a way for the checkpoints to notify the player when and where a checkpoint was set. That is for debugging only and not needed for this to work. The respawning feature can be seen in the gif above.