Galaxy Shooter — Health Collectable

Joshua Nielsen
4 min readJul 17, 2021

Greetings, and welcome to this special series of articles. In this series, the intention is to take a solid functional base game and add some extra features that (hopefully) make it just a little more special.

The base game we have here is called Galaxy Shooter. It’s a Galaga-style 2D shmup that I put together while going through the 2D Game Development course for GameDevHQ. It has a controllable player ship, enemies, powerups, sound effects, music, and more. It is, as stated above, a fully functional game that can already be played and enjoyed in its current state.

But let’s see what else we can make it do.

We’ve already given the player an opportunity to restore their ammo in the previous article. This time around, we’re going to make it possible for them to restore their health. Like with the ammo powerup, we’ll be adding a new powerup that replenishes the player’s strength.

How things work initially

Currently the player has three “lives” when the game starts. When the player is hit by either an enemy or enemy laser, they will take damage. If the player does not have a shield, this means losing one of their lives.

When the player loses a life, we see visual indications of the damage in both the UI and on the player ship. When they lose three lives, the game is over.

Providing a health powerup to the player means all of these things that happen from taking damage need to be reversible. That is what we will be working towards in this article.

Setting up the powerup object prefab

Much like with the ammo powerup we created in the last article, the health powerup needs all of the same things. Here’s a checklist of the steps I take for making any new powerup for the game:

  • Create a series of sprites for the new powerup
  • Import the sprites into the Unity project
  • Create a new game object using one of the sprites
  • Create an animation for the object using all of the sprites
  • Add the powerup script to the object
  • Configure serialized fields in the script
  • Add a rigid body to the object and make sure to turn off the gravity effect
  • Add a collider to the object and make sure it is set to be a trigger
  • Create a prefab out of the object
The new health powerup

With the prefab created, we’re ready to move on to the scripting work.

Scripting the health powerup

As stated earlier, what we really need to accomplish is to make player damage reversible. As things stand currently, the script assumes things are only going in one direction. In fact, the visual indicators on the player ship and in the UI are only updated in the section of the code that deals with player damage. We’ll need to make some fixes there first.

All of the updates to the player condition are made in the TakeDamage method

We’re going to want to take out both the call to the UI manager and the entire switch statement and put all of that code into its own method. That way we will be able to use that new method for both healing and taking damage.

The call to the UI manager is actually fine as is. In the UI manager’s script, we are simply replacing one sprite for another based on the number of lives.

The switch statement will need a few adjustments. For starters, there is no case for three lives. We’ll add a case for that condition, and the code block for that case will make sure that the engine fire animations are both disabled. Following that, we then need to add some code to the case for two lives that makes sure that one of the engine fire animations is disabled. Everything else should be fine here.

The new UpdateHealth method

After that work, the logic for the health powerup itself is very simple. All we need to do is check that the player isn’t already at max lives, and if they aren’t add one to the lives variable.

The code for the health powerup

This, along with the usual powerup sound and the new UpdateHealth method, takes care of everything we need out of the health powerup.

The health powerup restores one life and puts out the left engine fire

And that takes care of the health powerup! In the next couple articles, we’re going to be doing something very ambitious — we’re going to try to figure out how to make an enemy seeking missile powerup for the player. But before we do that, we’re going to develop a system for making some powerups less common than others. Until then, good luck and happy coding!

--

--