Feature: Health Pickup

Kyle W. Powers
4 min readMay 2, 2021

This article looks into adding a pickup that the Player can collect to increase their Health. This effect is pretty easy to implement since we already created a script used to interact with the Player when collecting a Power-Up and set the Player to take in an amount instead of subtracting one when the lives are changed.

In the PowerUp script, add an int for the heal amount of the health pickup; this is how much it will heal the Player. Then add Health to the Enum for the Power Up types.

Next, add a case for the Health element in the Enum to the switch statement and have it call the ChangeLives method on the Player and pass in _healAmount. That will tell the Player how to change their current lives.

Create a Prefab for the Health Power Up, like in previous articles. Set it to Health in the Enum, and make sure that the Health Amount is greater than 0.

In the Player script, we need to change a few things to prevent errors for the lives being out of range and turn off the damage visuals. Because right now, the Player can collect the Health Power Up, but if at 3 lives already, they will go to 4, and if at less than 3 lives, the engine fires will not turn off when healed.

Create a new int for the maximum amount of lives the Player can have; we will use this to prevent the Player from getting more than the maximum lives and be the variable set in Unity.

In Start, set _lives to _maxLives. That will have the Player start at the maximum lives.

In the ChangeLives method, when checking if the shield is active, check that the amount is negative; this will prevent the shield from blocking the healing effect and the health pickup disabling the shield. Then after adding the amount to _lives, create an if statement that checks that _lives is not more than _maxLives; if so, set _lives to _maxlives. Next, add an else if that checks if _lives are less than 0, and if so, set _lives to 0. The two statements will ensure that _lives will never be more than the maximum amount or go below zero.

Now, we have to set the damage visuals on the Player to turn off when healed. To do that, we will use an else if chain that checks for the value of _lives so that as it changes, we can update the damage visuals.

The first if checks to see if _lives are equal to 3, inside it create two if statements that check if the engine fires are active in the scene with activeInHierarchy if so deactivate them both. In the else if that checks for _lives to be equal to 2, after activating the first engine fire, create an if statement that checks if the second engine fire is active and, if so, deactivate it. In the else if that checks for _lives to be equal to 1, after activating the second engine fire, create an if statement that checks if the first engine fire is deactivated and, if so, activate it. The final else if is just the one checking if _lives are less than 1 and if so destroy the Player and stop the spawning of enemies from previous articles.

With that change, the Health Power Up now works as desired.

--

--