2D Space Shooter Game: Health Powerup

Manny Cabacungan
3 min readJul 28, 2023

--

Objective: Create a health collectable that heals the player by 1 ship. Update the visuals of the Player’s health to reflect this.

I’ve created a health powerup icon of my own:

  • Whatever image for the health powerup you have, import it into the Project pane and set it as a sprite as we did for the Ammo Powerup in the previous article.
  • Drag the Ammo Powerup prefab from the Project panel to the Heirarchy panel to make it into the Health prefab.
  • In the Heirarchy pane, rename the powerup to “Powerup_Health”.
  • From the Project panel, in the Sprites folder, drag the health sprite to the Sprite Renderer’s Sprite field in the Inspector pane.
  • Open the Powerup.cs script.
  • To the enum _powerupIDs, add “, Health” after the last item.
    public enum _powerupIDs
{
TripleShot,
SpeedBoost,
Shields,
Ammo,
Health
}
  • For the Powerup_Health prefab, in the Powerup (Script Component), Inspector panel properties, change the Powerup ID to “Health”
  • Make the Health Powerup into a new saved prefab. In the Hierarchy pane, drag the Health Powerup down to the Project pane, into the Prefab/Powerups folder and make it as a new original prefab.
  • In the Hierarchy pane, select the Spawn_Manager game object.
  • In the Inspector pane, expand the the “Powerup Prefab” array.
  • Increase the size of the array by one.
  • In the Project pane, drag the “Powerup_Health” prefab to the Element 4 field.
  • Open the Player.cs script.
  • Add a new method for adding a new ship to the player’s life. Use an if conditional statement to check the variable _lives if it’s less than 3 then increment _lives by one and call the UpdateLives() method from the UI Manager script
    public void AddLife()
{
if (_lives < 3)
{
_lives++;
_UIManager.UpdateLives(_lives);
}
}
  • Open the Powerup.cs script
  • In the method OnTriggerEnter2D(), in the switch case statements for the _powerupID variable, add a case block for “_powerupIDs.Health” and call the AddLife() method in the Player script.
switch (_powerupID)
{
case _powerupIDs.TripleShot:
player.TripleShotActive(_powerupDuration);
Debug.Log("Powerup::OnTriggerEnter2D:switch _powerupID=0 TripleShot");
break;
case _powerupIDs.SpeedBoost:
player.SpeedBoostActive(_powerupDuration);
Debug.Log("Powerup::OnTriggerEnter2D:switch _powerupID=1 SpeedBoost");
break;
case _powerupIDs.Shields:
player.ShieldsActive();
Debug.Log("Powerup::OnTriggerEnter2D:switch _powerupID=2 Shields");
break;
case _powerupIDs.Ammo:
player.AmmoRefill();
Debug.Log("Powerup::OnTriggerEnter2D:switch _powerupID=3 Ammo");
break;
case _powerupIDs.Health:
player.AddShip();
Debug.Log("Powerup::OnTriggerEnter2D:switch _powerupID=4 AddLife");
break;
default:
Debug.Log("Powerup::OnTriggerEnter2D:switch No_powerupID");
break;
}

Now when you test, after losing a ship and collecting a health powerup, a new ship will be added to the life UI.

--

--