Adding mine attacks to our boss
In this article we’ll look at the various mine attacks we’ve implemented into our boss wave.
We’ll need some variables as always, including speed for various mine attacks, whether any mines are alive, and some arrays to hold the actual mine game objects once they are instantiated.
Note that in our Update() method, the very first thing we do is call CheckMinesAlive().
Since mines don’t have a set expiration much of the time, due to player seeking behavior and the ability to be killed by the player, it is important to find a way to keep track of the mines that are alive.
This method creates a couple of temporary arrays to hold any mine game objects found using GameObject.FindGameObjectsWithTag().
Since we have two different tags in use for mines, we make sure to grab an array for each.
We then check if the arrays are null, and if so set our _minesAlive flag variable to false.
If not, we set it to true.
If these arrays don’t show as null, we can also check their length.
If 0 in length, we can set _minesAlive to false.
Once again, our Update() method is where we trigger our boss actions, in this case mine attacks, by calling DeployRandomMineAttack().
We have a 40% chance here of deploying mines, and we’ll add further random variation in the DeployRandomMineAttack() method.
Now below our boss action if / then block, we also check if _minesAlive is true, and call MoveMines1(), 2, and 3 if it is.
Our MoveMines() method checks if the associated array is null and if each field in that array is null.
If not, it takes the mine stored in the current field and moves it to the appropriate destination.
In the case of Mine Attack 1 or Mine attack 3, that is towards the player as these are player seeking mines.
In the case of the Mine Attack 2, we move them out in a pattern that resembles a half circle as shown above by declaring destination variables with the appropriate Vector3 positions.
Similar to what was discussed in the Laser attack article, I’ve placed game objects in the scene as target locations for the Mine 2 Attack.
I wrote down the transform positions for each of these game objects and then deleted them from my project.
mine1Destination, mine2Destination, etc holds these values and sends 1 mine towards each of these target locations.
Since these are not player seeking mines, they may very well end up off player screen without dying and then just, keep on floating forever.
That’s why in this MoveMines2() method we each mines position as we move them, and if a certain distance off screen in any direction, destroy the game object.
The DelayDeployMine() coroutine allows us to deploy mines in a sequence rather than all at once.
Using Random.Range once again, we set the odds for executing either Mine Attack 1, 2, or 3 as well as what type of mine will be used in that attack for added variation.
Freeze mines being mixed in makes for more interesting gameplay since they won’t damage the player themselves, but stunning the player for a few seconds makes them likely to take damage in other ways or prevent them from getting much needed powerups.
Our Mine Attack 1 method is very basic, and not meant to be a serious threat to the player.
It pops out 3 player seeking mines that could either do damage or will be freeze type that stuns the player depending on the DeployRandomMineAttack()’s random numbers generated.
Since we know that the last DelayDeployMine coroutine is called at 9 seconds, calling EndBossAction coroutine with a delay of 10 seconds is appropriate.
Our Mine Attack 2 method essentially spits out 5 mines in a half-circle pattern at the target positions previously written down as detailed above.
Since this attack is rather quick, we call EndBossAction coroutine after a relatively short 4 seconds.
Our 3rd mine attack method is very simple and is meant to be an easy break for the player in between other harder boss actions and attacks.
2 player seeking mines, always freeze type, come out and attack the player.
We don’t want the player to milk these mines and leave 1 alive just dodging it and leaving our boss not moving onto another attack, so we set an 8 second delay on our EndBossAction() coroutine.
Just as a reminder, even though shown in previous articles, our EndBossAction() coroutine is very basic just setting a flag variable for boss actions after a specified delay.
That’s it, our boss now has a ram attack, various laser attacks, and a variety of mine attacks.