Giving our boss a proper send-off
The last thing before integrating our boss battle is to give our boss a satisfying end!
Just like a boss ship from a long time ago in a galaxy far far away, we want to reward our player with a satisfying end to our boss ship. The code is already set up to easily implement it, and we already have elements from a prior enemy ship we can reuse, so let’s begin!
Under the main Boss empty, create a new empty and name it DestructionHolder.
Inside the DestructionHolder, create another empty called DestructionBox and attach the MiniBoomalypse script from Enemy 4.
Duplicate the destructionBox 5 more times and place each one in the middle of each section:
Then drag each section into the parent slot which I named _papi for snits and giggles. The miniboomalypse needs there to be a parent so it knows where to generate the mini-explosions. After you’ve done this for all six sections, we’re almost ready! Just deactivate it for now and we’ll turn it on when it’s needed. We just need to add a little bit of code to the main boss script.
Just to keep things simple, I created a holder called _destructionHolder where we can drag our boss’s DestructionHolder object into it via the inspector.
In the IgnoreSectionNow function that we went over during the section cycling article, I added a brute-force approach to knowing when all the sections were destroyed. I created an int called _deathCount, and every time a section is destroyed, we add one to it. When all 10 sections are destroyed, we call the Die() routine.
Here, we activate the DestructionHolder. The miniboomalypse is always running, so there’s no need to initialize it.
We’ll also have our boss ship fall. Even though there’s no gravity in space, We’ll just say it’s the force of the explosions pushing it down.
we’ll use a variable x to hold 15, that will be the number of seconds the boss ship has left before it explodes for good.
Then we do a while loop checking x to see if it’s over 0 and during that time, we’ll move the ship downwards using transform.forward with a speed of 2 multiplied with Time.deltaTime. Usually you don’t hardcode values like this, but this only happens once per game so it’s fine.
And just for fun, we’re going to rotate our ship very slightly.
Then we’ll subtract time.deltaTime from x and our timed countdown begins!
When we’ve passed the 15 second mark, we instantiate an explosion we have handy in our Resources folder.
We then parent the explosion to the main ship. We’ll also keep the explosion on top of the ship. If we don’t do this, the explosion will be placed in a place that doesn’t look natural. This way, it’s always on the Boss empty every frame.
Since the ship is now quite a ways below us, that means the explosion will reflect how far away it is too by being smaller so we’ll manually make it bigger
All of this happens in a frame or two, so we’ll see a huge explosion. During the explosion, we’ll destroy the boss ship as well.
All we need to do now is integrate it into the game.
But that’s it for now, let’s see it in action!