Daily Progression Update Lives and how to remove them!

Objective: Create a Lives system for a 2.5D platformer!

In the past I’ve shown you how to create a life and death system. In the arcade shooter, we would loose a life when the player was attacked. In this tutorial We are going to create an invisible dead zone and we want to loose a life when our player enters or passes through this invisible wall.

To begin, Create a cube, place it below your level, and stretch it to the length of the level you’ve created.

Make this a trigger. Turn off the mesh renderer and add a rigidbody. We don’t want this zone to move so turn off the gravity. Create and attach a script for the dead zone and this is going to be a trigger script. We want to check for if the player collides with this object.

What do we want to do when the player falls off the platform and passes through the dead zone? We want to do two things, remove a life from the player and respawn in the beginning of the level. First lets tackle the life system. On the player script we need to create a variable for life and create a damage method to remove a life.

Simple, when the damage method is called the amount of lives is removed by one. We need to also create an UI text element to display the amount of lives the player has available. Duplicate the coins element and drag it above.

Change the text to ‘Lives:’ and rename the object. On the UI script we need to create a new text and int variable and attach this lives object in the inspector.

As we did with the coins we need to create a method on the UI Manager class that will update the lives text displayed using an int parameter.

We need the player to call this method in two instances. In the start method we need to set the default amount of methods, and we need to update the lives displayed in the damage method.

Going back to the dead zone class we can call the player damage method, this method will call the UI Manager and the lives will be affected accordingly. One life is lost when the player falls off the platforms and passes into the dead zone.

Now, on to the second response we want to have happen when the player falls into the dead zone. We want the player to respawn in the beginning of the level. Create an empty object and rename “Respawn_Location.” Copy the players start location and paste the values into the respawn object. On the dead zone class create a GameObject variable representing the respawn object and attach in the inspector.

We need to set the reaction on the dead zone to change the player’s position and set it back to the respawn position.

When you test play your game you’ll notice that the player will pass through the dead zone and not respawn. This is because your player is falling too fast. To fix this we are going to create a handle to the players character controller. when we have reference for this we are going to disable the controller and this will grab your player and bring them back to the respawn.

Your player will now respawn to the beginning although they will be stuck because we disabled the character controller. A simple method to fix this is with a coroutine. We can create a coroutine that waits for 0.1f seconds and then re-enables the character controller. With this being outside of the on trigger method you can use a parameter to pass in the character controller.

Now at the end of the on trigger enter just call the coroutine.

Now you character will loose a life and respawn. If you like in the player damage method you can set it to when the players life is less than 1 you can reload the scene. Add the scene manager library and load the scene your are currently on.

There you have it. You can now play your game, and if you fall off your player will respawn and loose a life. After loosing 3 lives the game will reload. Don’t be afraid to experiment with this code and I’ll see you in the next tutorial!