Phase II: Boss Enemy, Part 2
Today I continue working on the Boss Enemy for the 2D Shooter game.
Objective: Set up a health system for the Boss Enemy, detect when the Bos is hit by a player weapon, show explosions when and where the boss is hit, and create a UI display that will show the boss's health.
A health system is simple enough. First, I need to create a health variable for the Boss Enemy then decrement the value whenever the boss is hit.
This is the variable I’ll use for the Boss’s health. I’ll serialize it so that it can be changed easily in the inspector to fine-tune the health level for the Boss.
Next, I’ll use a method to decrement the Boss’s health and call it when the Boss is hit.
Now that the basic health system is set up for the Boss, I want to detect when a player's weapon hits the Boss Enemy. For this, I’ll use the OnTriggerEnter2D event.
Since I use two different tags for my player weapons, which apply to either lasers or the Homing Missile, I will detect them separately. This will also allow me to use different-looking explosions for each one.
Then, inside each condition, I will instantiate the relevant explosion gameObject, change its scale to be about .5 original, destroy the other gameobject, then call a method HitByPlayer() which will reduce the Boss Enemy’s health by 5 for lasers and 10 for the missile.
The final piece of the puzzle is to provide a UI display to show how much the Boss is hurt. I’m going to repurpose the bar I used when I created the Thruster UI. I’ll duplicate it and change the name to BossHealth.
Then I'll create a new script for the BossHealth slider.
In this script, I have two methods. The first SetBossHealth() sets the slider to the starting health for the Boss, and the second, DamageBoss(), adjusts the health of the Boss. This will be called from the Boss when they are hit.
In the boss’s script, I get a handle on the health bar for the Boss.
Then finally, I modify the HitByPlayer() method in the boss to call the DamageBoss() method on the slider.
This gives me a working damage system for the boss, with effects and a UI to display the damage the Boss is taking.
That does it for this article. In the next article, I’ll start setting up attacks for the Boss to destroy the player. The goal is to have two basic attacks and a more powerful special attack.
I hope you found this interesting, and until next time I wish you luck on your coding journey.