2.5D Platformer — #6 Lives and Respawning
Objective: Give the player lives and respawn when they die
This platformer is now ready to have player lives introduced. A player with lives must have something to remove the lives and for this, we will add a kill zone at the bottom that span the whole level. We need text on the screen to display the number of lives the player currently has and if the player has no more lives, restart the game.
First, let's create a variable to store the number of lives
Let’s add UI TextMeshPro to display the lives
Let's create a cube, rename to Killzone and span it the whole level and position it at the bottom. Turn off the Mesh Render and check Is Trigger. Create a Tag and call it Killzone and set the Killzone as Killzone tag.
In the Player script, create OnTriggerEnter. Here we detect if we have touched the Killzone, reduce life and trigger an event similar to the CoinsCollected. If the life is zero, restart the game otherwise, respawn.
Change the UI Manager to subscribe to the event and update the UI.
Drag the LiveText to the UI Manager reference
Finally, we will need a Spawn Point, create an empty and name it Spawn Point.
Create a reference to it and drag the Spawn Point onto the Player script on the Player
Make sure the current scene is in the Build scenes as index 0.
If you enjoyed reading this article give me a Clap, also if you would like to see more, “Follow” me, so you may be notified of future releases. You may also send me a message if you need any further help.
Do you have a project in mind that you need help with, I’m available!