Life Recovery Via a Health Powerup!
Objective: Create a powerup to heal your player.
A great way to extend someone’s play time is the ability to regain lives lost. Today we are going to add a powerup to give the player just that. The powerup is going to give the player life back up to the max of three lives. To begin we need to make a sprite with the components of the collider, rigidbody, and the script that controls your powerups.
I’m using a scaled down sprite of the player and making it green.
Open the powerup script and add the ID# to the switch statement with a method we are going to create next.
We need to adjust the size and add the sprite to the inspector of the spawn manager.
On the player script add the method that the powerup calls and inside we need to decide what the powerup does. We want to add a life to the player, as well as the UI manager.
This works great however, as it stands now the player’s lives need to have a max limit so in the UI manager script we need to add an if statement to limit it to 3.
Now the lives can only go up to 3 and the game is over whenever the lives go below 1. The last thing we need to do is turn off the player damage when we get a life powerup. To do this we need to add an if statement on the player script where the powerup calls when activated.
This way when the lives go from 1 to 2 the right engine damage is hidden, and the same for when the lives go from 2 to 3.
This is all there is to it and now your player can regain their lives with the powerup we created. If you are using more than 3 lives you can add whatever values, you need. Don’t be afraid to experiment with your code and I’ll see you in the next tutorial.