Adding a health powerup to our prototype
The first thing we are going to do is create our Health_Powerup game object and add several components.
We need rigidbody2D, CircleCollider2D, the Powerup script, and AudioSource components.
I went ahead and changed the sprite color of the Health_Pickup to a nice yellow, created a prefab, and deleted the Health_Powerup game object in my hierarchy.
Now, I want to get an issue out of the way. The powerups I’ll be adding won’t require child game objects to hold multiple sprites like our Ammo recharge did.
Since I know I have an if statement for that circumstance based on the _powerupID value being less than 3 or greater than 2, I want to update it with Ammo recharge being an ID value of 5.
We are adding a shield collectible in this article, but I also intend to add another Secondary Fire powerup in the next article that may or may not require child game objects.
So it makes sense to make Health 3, Secondary Fire 4, and Ammo recharge 5.
Modifying our Powerup script
We’ll start by redoing and adding to our Powerup ID comment line for what values are going to represent what in thee Powerup script.
Scroll down to the OnTriggerEnter2D() method and update the switch statement to reflect our new values and the actions that will be taken.
We change our if statement to reflect our new thought process, changing it to less than 5.
In the Unity editor, we need to change the value of the Powerup ID on tour Ammo_Charge_Powerup prefab.
We also need to change our Health_Powerup prefab to have a Powerup ID of 3.
Modifying the Player script
We add a new public method to our Player script, AddLives() with an int lives argument so that we can a lot the number of lives we want to add rather than only allowing 1 life at a time to be added.
We never want our player to have more than the maximum of 3 lives, so we use an if statement to determine if they are already maxed out on lives before adding the correct number of lives.
Wrapping up our Powerup script
Now all we need to do in the Powerup script is call the Player script’s AddLives() method and pass a value of 1.
Modifying the SpawnManager script
As always, we need to add a variable.
This will hold our Health_Powerup prefab game object.
Using our prior routines as a template, we create a new SpawnHealthRoutine method.
I suspect the way I have this routine structured to take in the necessary variables, we could have this same routine named something more generic and call it multiple times with different input variables.
That’s not important to me at the moment, but I’ll let the percolate in the back of my mind for now.
Lastly, we add another StartCoroutine for the newly created SpawnHealthRoutine and give it some spawn time intervals.
I don’t want the player tripping over lives, so this has the longest min and max spawn interval values.
Don’t forget to assign the new Health_Powerup prefab game object to our Spawn Manager script component.
Modifying the DamagedFire script
Now, when our player loses a life currently, we show damage by activating 1 or 2 hidden game objects that show damage on our player ship.
So, we have an public ActivateDamage() method, let’s add a DeactivateDamage() public method.
Back to the Player script
I want to “rewind” the damage so to speak, to I’d like to do it in reverse order of how it was applied.
Looking at the LoseLife() method in our Player script, we see that when the player lives drops to 2, _damagedFire1GO is activated.
Down to 1 life, _damagedFire2GO is activated.
We want to add an if statement checking the current player lives before we add a life and disable the correct visual ship damage by calling on our new DeactivateDamage public method on our DamagedFire script.
We also need to call our UIManagerScript and update the UI elements that represent player lives.
Looks like I missed something! Let’s fix it.
Our Health_Powerup game object prefab needs to have the power_up_sound clip assigned to the Powerup script component.
Alright, everything should be working.
Take it for a spin!