Creating a Boss Enemy — Part 4, Health System
Our Boss now has all the necessary things to make it a formidable opponent, but as of now, it’s invincible!
In this article, we’ll create a Health system for our Boss, and a unique explosion that triggers when you defeat it!
Boss Health Bar UI
Creating the Boss Health bar is very similar to when we created the Player Thruster UI element. You can read up on that here: https://frankgwarman.medium.com/small-speed-boost-with-player-thrusters-ec0fd3a67b0a
First we’ll create the UI Elements needed inside our Canvas; a Border, a Fill, and Text to tell the Player who’s Health Bar it is:
I’ve nested these element into a Parent Object to make enabling/disabling them in the future much easier!
Now we can jump into our UIManager script attached to our Canvas UI. We’ll need to make two reference variables; one for the Boss fill to update the actual Health Bar, and another to enable/disable the entire Boss Health Info:
And utilize these variables in two new functions:
Boss Health in the Boss script
Let’s jump back to our EnemyBoss script to complete our Health Bar logic.
First we’ll get a reference to our UIManager and initialize at Start():
Now we can create our Global variables for the Boss Health:
Next we’ll create a function to call when the Boss will spawn that will enable the Boss Health Info UI objects, as well as set our Boss Health to full:
Then we can create our OnTriggerEnter2D logic. Each time the Boss gets damaged, we’ll decrease their health and update the UI:
Finally, we’ll create a function that checks if the Boss Health is at zero. This will be called in Update(), and if called will cause an explosion sequence, disable the Boss Health Info UI elements, and start the process to begin the next wave of Enemies.
- You could also end the game after you destroy the Boss if that’s what you fancy
For the explosion sequence when the Boss gets destroyed, I reused the previous explosion VFX we created for the Asteroid. I also Instantiate 3 of those explosions, changing the colour, scale, and position for each one to give the explosion some depth!
Then the ExplosionSequence():
When to Spawn the Boss
This is completely up to you when you want to spawn in the Boss. You could do it every 5 waves, only once and continue playing (like I did), or as the last Wave before “Winning” the game.
Handling your Boss Spawn will be done in the SpawnManager script:
For me, I arbitrarily chose to spawn the Boss after wave 7. After play testing my game, this seemed like a level of exhaustion where a Player would not be expecting a Boss to pop up. I like that essence of spontaneity.
And now, the Boss is finished! Adding a ‘Boss Enemy’ into your game is a great way to make it more memorable and add to the difficulty.
With our Game essentially finished, the last step is to build the game and show it off to the world! In the next, and last article of this series, we’ll cover those basics, and where to host your game!