When creating the Boss Battle, I wanted to break it down into 3 parts: The Shield, Firing & Movement. The purpose of the shield is to protect the Boss from receiving any damage until the Player has destroyed all of the Shield Generators moving around it.
The Shield Generators have two movement patterns. The first pattern is moving in a clockwise direction then moving in a counter-clockwise direction. The second pattern is the orbit speed will increase then decrease. The hit generators can only take one hit, but this can be adjusted in the Inspector.
The Boss Shield cannot take any damage until ALL of the Shield Generators are destroyed. When ALL of the Shield Generators are destroyed then it will be able to take damage. The Boss Shield can take 9 hits before being destroyed and at 3 hits it will change color to let the player know it is weak.
The Shield Generators Code Breakdown
All of the Shield Generators are children of the Boss Ship. This comes in handy when I create the Boss movement, because child objects move with the parent object. But, we are only concerned for the rotating around the boss ship. This requires getting the Boss’s position so we will have to get a handle on the Boss by declaring a GameObject and…
assigning it in the Inspector.
For the rotation movement we need three data points: a point to rotate around, the direction to rotate around that point and a speed to rotate at.
This can done using transform.RotateAround and using the Boss’s position for the rotate around point, Vector3.forward for the direction and a value * Time.deltaTime for the speed.
The rotation pattern will have the Shield Generators rotate in a clockwise direction (Vector3.forward) for 20 seconds before having it move it a counter-clockwise direction.
In the order for the Shield Generators to rotate in a counter-clockwise direction we only need to change the direction from Vector3.forward to Vector3.back.
Making the rotation change directions, we need a bool variable (_rotatingClockwise) that will keep track of the direction it is moving and will be checked in the Update.
Now lets create the change of orbit speed. Coroutines are need because we will need to wait for some time before executing certain behaves. There is a coroutine to handle increasing the orbit speed and decreasing it. In each of these coroutines we use to a While Loop to either increment or decrement the orbit speed until we reach either our max orbit speed or min orbit speed.
In the Update, we check to see if the orbit is less than the max orbit speed and if it is we will start the increase orbit speed coroutine and if not we start the decrease orbit speed coroutine.
Lastly, we use an OnTriggerEnter2D to detect if a Laser has hit the Shield Generator and it has we will take health away and check to see if the health is at 0. If it is, we will destroy the Shield Generator object.
The Boss Ship Code BreakDown
With the Boss Ship script, the first thing we want to know is if ALL of the Shield Generators have been destroyed. So, we will create a bool method that will search for game objects with a “ShieldGenerator” tag and if the search comes up null that means ALL of the Shield Generators have been destroyed. We only want to perform this search every second beacuse it takes up a lot of memory if it being performed every frame, so we use this search countdown variable which countdowns from 1 and we it gets to 0, it is reset back to 1 and the search is performed. If ALL of the Shield Generators are destroyed it returns false for the Shield GeneratorsActive method.
Once that method returns False it sets a shield vulnerable to True. This means the shield can now take damage. Using an OnTriggerEnter2D, we check to see the shield vulnerable variable. IF it is False only the laser is destroyed, but if it is True the laser is destroyed and the shield health loses 1.
When the shield health is less than 4, we change the color of the shield and when it gets to 0, the shield visual is deactivated and the collider is destroyed.
In order to deactivate the Boss Shield visual we need to get a handle on the game object by assigning it in the Inspector and we need to get the Renderer for the Boss Shield, so we can change the color. Once we got these, we just need to initialize them.
This is it for setting up the Shield for the Boss. Cheers!