Creating a Health PowerUp

Daniel Jennings
3 min readAug 15, 2021

--

Let your Player Heal!!

Task:

Create a PowerUp that heals the player.

Plan:

Similar to our Ammo PowerUp, we will need to add the PowerUp as a Spawnable object, make it collectible by the Player, and add functionality to the Player and UI once the PowerUp is collected.

Process:

We will keep the max lives at 3 for this upgrade.

Phase 1 — Add PowerUp into existing framework:

First stop: PowerUp.cs

Let’s add our new PowerUp tag.

Then add to our PowerUp collection logic. Based on this you can see we expect our Player object to handle this event. Let’s setup that logic next.

Having to add to two switch statements every time we add a spawn object turns out to be very redundant. Let’s look into how to fix this.

This was the code path of our call into Player which eventually calls CollectHealth. Let’s make CollectHealth public so we can stop adding to the redundancy mess.

Old pathway into and through Player.SetPowerUp
New pathway direct to Player.CollectHealth

Here is our new method which handles updating of life counter and the UI. Since we are leaving the life cap at 3 let’s add a check on collection of the Health PowerUp. In case we heal with damage we should update the thruster damage on the UI.

We will clean up the rest of the Player.SetPowerUp method at another time so we can focus on finishing up our HealthPowerUp.

Damged
Space Hearts are Awesome!!

Code Clean Up

Like I said, we would clean up our code later… Well, now is a good time.

Here is the new path through PowerUps:

And the new path through Player:

--

--