Lives and Respawning
For today's class challenge, we’re going to be implementing a life mechanic if the player falls off the screen.
There are a few ways we can do it, we can try putting in an empty below the scene with a collider box and then remove a life when the player triggers it.
That takes a few extra steps though, I think I’ll just remove a life and respawn the player when the player falls below a certain number on the y axis.
I’ve moved the Player far enough where I think it’s a good death area. -12 seems to be the magic number in the y axis, so that will be the dying point for our player, but first let’s set up a lives system.
In the player script, we’ll begin with the variable _lives.
Next, let’s make a variable to hold the y axis number where our player will lose a life.
Now let’s make a check for when the player moves past that line. Let’s make a function for CheckIfDead(), and we’ll put it in our update too.
I didn’t put any code in to subtract lives yet because I want to make sure if it works, so let’s check!
Ok! It works! Let’s start reducing lives!
We should add it to our UI canvas so let’s duplicate the coinsText and rename it to LivesText and move it up a bit. Of course, we’ll change the text to something like “Lives:”.
Now we’ll make a holder of it in the UIManager as we did with CoinText.
And drag it in…
The function to update our lives is an exact repeat of the coins function, so this will be familiar.
Now we’ll put the code in the player to subtract lives but first, we’ll initialize the UI in start.
Let’s see if it worked:
Woot! Woot! Time to subtract some lives!
We need to update the UI manager any time there’s a change, so we’ll just add the line to do that right after we subtract a life from the lives variable. Let’s see if it works!
Whoa! That’s too many lives lost! what if we immediately put the player back somewhere?
Ok, that’s acceptable for now. If we want the screen to hold after one dies, we may have to create a boolean to say the player is dead to prevent any more lives from being subtracted, but this is an acceptable fix for now.
And with that last check, we’ve already partially solved respawning, but let’s do it slightly better. Let’s use an empty as a waypoint.
Place an empty on the screen where you want the player to respawn and call it ‘SpawnPoint’ or somesuch.
Next, we’ll have Unity look for it with GameObject.Find, and then spawn the player at SpawnPoints position!
Let’s see if it works:
It seems to work well, but I think I’ll feel better if we make sure it only takes one life, so let’s put in some safeguards:
let’s make a boolean called _isDead;
Next, we’ll move the respawning code into its own function called Respawn().
And we’ll call respawn in our former CheckIfDead code.
Now we’ll put in our safeguard, we’ll use our _isDead boolean to make sure we only subtract one death.
Since CheckIfDead is in Update, we’re going to be checking if the player is dead 60 times a second. This makes sure if we remove a life, it only happens once until we set the boolean _isDead to false again. When can we do that? Probably when the player is safe and sound, like..where the player Respawns!
And just to be sure physics is working correctly, let’s change Update() to FixedUpdate().
Great! Let’s see if things are working now:
Awesome! We’re now only losing one life every time we fall! However, there’s one more trick I’d like to share and that’s to freeze the screen for a second before respawning, I’ll go into that tomorrow, see you then!