Adding a Boss to 2D Galaxy Shooter in Unity-Part 04-Boss Health & Shield System!
Objective: To understand and implement how to create a shield and health system for our 2D Galaxy Shooter Boss.
As mentioned in Part 01, I have implemented a shield and health system for the boss but with a slight twist. Instead of player’s normally trying to destroy the shield first I have made it so that the player’s lasers in fact give it energy back to the shield, so the player actually just needs to survive while the boss continually shoots all it’s weapons and it drains it’s own energy at which time when it hits 0%, the player will have 5 seconds to throw whatever they can at it. Let’s get into it…
Creating Some Variables
Let’s main variables we need are shield/health min and max health and also a percent that we are going to keep track of through Unity’s UI sliders similar to the ThrusterGauge in this article:
Then we are going to check in Update() whether the boss’s shield is less than or equal to 0 and if it is, then we are going to run a coroutine that handles a bunch of UI changes.
OnTriggerEnter2D() — Trigger Checks
Here is an example of a trigger check I have setup for the “Laser” tag. There is a series of If checks to first see if the boss is activated and we can attack it, then we check to see If the shield is activated on the boss, if it is then we are going to add 25% to the boss’s shield. Then the next If check is going to see if the shields % is greater than 100 and if it is set it to 100%.
else, the shield isn’t activated, meaning we can damage the boss’s health so we damage it for 25%.
else, if the boss isn’t activated, do nothing.
BossShieldDamage() and BossHealthDamage() Methods
Finally, these are our BossShieldDamage() and BossHealthDamage() methods that get called when an object triggers the boss/boss shield colliders. They both take 1 parameter that is a damage amount and this then reduces the shield/health by that amount.
There is also an If check to make sure that boss’s shield/health don’t fall below 0f and if it does, then we simply make it equal to 0f.
Voila — Boss Implemented and 2D Galaxy Shooter Finished!