Nerd For Tech
Published in

Nerd For Tech

Creating Life mechanic and Modular Checkpoints

Next up, we need to add a system to our game that will give the player lives to work with along with respawning at the beginning of the level whenever they fall off of the map. To start, we will get the lives counter in place, along with the addition of the code so the text can be updated:

From here, we will go into our scripts and add in the variable for lives within our player, along with the script in our UIManager to have the text be adjusted:

With our base code in place, we now how to look at how we can create the system for our player to respawn at the start of the stage, or restart upon the loss of all lives.
Before we go about and code in how we will create a respawn situation, we first need to create a few objects so that we can have a modular system for our respawning:

First, we will create a drop zone that will be our trigger for if the player has fallen off the map. This will allow us to create our maps with varying levels of height, allowing a larger scope to work with. Next, we need to create checkpoints that our player will respawn at depending which platform they hit:

With these in place, we can now look to creating our script to work with. For this, we will create a new script that will handle most of the processes for when we fall:

The purpose of this script is to identify if the player has hit our trigger area. If it has, we want to communicate with our player script to take away a life. What we also want to do is be able to turn off the charactercontroller. This is because what happens if we start falling, our players velocity is so quick that it will bypass the trigger event and just fall indefinitely. In order to turn the controller back on however to let us regain control of the player, we need to create a Coroutine that will enable to controller again after half a second. Finally, we need to use the checkpoints we have created and tell Unity that if the player activates the trigger, we want to go back to that checkpoint.
Finally, we have a few additions to the player script in order to have the lives work properly:

What this does is tell Unity that whenever we hit the trigger, we want to lose a life, and whenever our lives is less than 1, we want to reload the scene, allowing us to start over. With all of this in place, we can now see if the player goes back to the beginning, and if our player’s lives and coins reset upon death:

With this working, we can now look to create a modular system for the drop zones. This will allow us to give the player a small relief if they make a mistake, bringing them back to the last checkpoint over just restarting all over again. All we simply need to do is create a new zone, a new checkpoint and test it out:

Now that we have the ability to restart from checkpoints or start from scratch, it’s time we start looking at some more additions to the movement in our game.



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