Feature: Boss Enemy — Part 1
The next feature we will add is a Boss Enemy that spawns at the end of the last Wave. The creation will be divided into three parts. This article will focus on the weapons of the Boss Enemy.
Weapons
For the Boss, we need to create a new Laser Beam weapon and edit the Homing Missiles to target the Player.
Laser Beam
The Laser Beam is simple to implement. The script will be on the Beam checking for the Player to collide with it and then damage the Player.
Beam Emitter
The Beam Emitter is what will be used to enable/disable the Laser Beam and can be destroyed by the Player. For this script, we need a few variables, an int for the starting health of the Emitter, an int to keep track of the current health, a GameObject for the Laser Beam, and finally, a GameObject for the Explosion Prefab that spawns on destruction.
In Start, we set the current health, assign the Laser Beam as an Enemy weapon to damage the Player, and turn it off to hide the Beam until the Emitter is called to fire.
Now the Emitter needs two methods, one to take damage and see if it should destroy the Emitter, the other to enable/disable the Laser Beam.
The last thing the Beam Emitter needs is OnTriggerEnter2D to check the Tag of the other Collider and respond accordingly by either damaging the Player, destroying the incoming Laser, or only take damage.
What the Beam Emitter Prefab looks like set up in Unity.
Beam Emitter in action, damaging the Player and able to be destroyed.
Homing Missiles
For the Homing Missiles, we will add on to the Player’s Homing Missile script and then make a new Prefab for the Enemy Missile.
We need to add a few variables to the HomingMissile script. The first is a float for the amount of time the Missile has before it is destroyed, so it does not last indefinitely. The second is a float that we will use to track how long the Missile has been active. Then a GameObject for an Explosion Prefab to spawn when destroyed and a bool to know if it is a Player or Enemy Missile.
In Start, we check that it is not an Enemy Missile before searching for an Enemy target.
In Update, we keep track of the time the Missile has been active and destroy it if it is past the fuse timer.
In FixedUpdate, we again check if it is an Enemy Missile before getting a new Enemy target if the target is null. And for everything else, we stop rotation on the Rigidbody so the Missile will fly in a straight line when it doesn’t have a target.
Next, we need a method to assign the Missile as an Enemy Missile that takes an argument for the Player’s Transform and sets the target.
The last thing needed is an OnTriggerEneter2D to detect the collision with the Player if set to Enemy Missile.
The Enemy Missiles in action track the Player and explode after being active for long enough.
In the following article, we will finish creating the Boss Enemy.