Geek Culture
Published in

Geek Culture

2.5D Platformer: Adding Lives Display and Respawn

Today, I’ll add a lives display to the game and have the Player respawn when they fall off a platform.

First, I’ll create a variable for lives in the Player script and set it to 3.

Then I’ll duplicate the Coin Text under the canvas and change the name to Lives and change the text in the textbox to “Lives:”

Then I can move it up above the Coins in the UI window.

Next, in the UIManager script, I’ll add a reference to the Lives text and create a method to update the Lives display.

Then in the Start() method of the player, right after I null check the UIManager, I’ll call the UpdateLivesDisplay() method.

Also, I need to drag the Lives GameObject into the Lives Text for the UIManager in the Inspector.

That sets up the UI element for the Lives display.

Next, I needed to create a way to detect when the Player has fallen off of the playing field. To do this, I created a 3D cube and scaled it on the X-axis to about 10. I then changed its name to Dead_Zone and set it as Is Trigger.

O also created a script called DeadZone to attach to the Dead_Zone object. Since I hadn’t decided how far I was going to make the platformer, I wanted to make the Dead_Zone follow the player along the X-axis so that I could build the level infinitely in either direction along the X.

To do this, I created a serialized Transform on the DeadZone script.

Then in the Update() method, I have the Dead_Zone follow the player on the X-axis while keeping its own Y and Z.

Then I just needed to drag the Player gameobject into the appropriate place in the inspector.

Now it’s time to have the Dead_Zone detect the Player. I do this by setting up an OnTriggerEnter() event in the DeadZone script and checking if the object is the Player. If so, I’ll call a method on the Player named PlayerDied().

Now, in the PlayerDied() method, I’ll decrement the Player’s lives and call the method on the UIManager to update the lives. Then I’ll check if the Player has zero lives left. If so, I’ll restart the level. Otherwise, I’ll spawn the player back at their spawn point.

Now, this almost finishes the respawn and lives counter section. But there is a small problem. It looks at if the Player is moving too fast to have their position reset when they fall. So what I will do is turn off the CharacterController component first thing when the PlayerDied() method is called. Then I’ll create a co-routine to wait .1 seconds before enabling the Character Controller again. The co-routine will be called right after the player is respawned.

And below is the co-routine that will be called.

Now the Player will respawn when they die, and the game will restart at zero lives.

I hope you enjoyed this article, and make sure to check out the next where I’ll start setting up some puzzle elements for the game.

And as always, I wish you the best on your coding journey.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store