Boss Attacks

This tutorial covers giving the boss two different attacks. The first attack will fire a spread of lasers across the screen. The second attack will fire a series of lasers at the player.

The first step is to update the Laser script. Since the lasers are no longer shooting just up down but also side to side they will not always be destroyed when they go off screen. That needs to be fixed. Create a new function that will check each edge of the screen to see if the laser is beyond it. If it is Destroy the laser. You will then replace any checks to see if the laser has gone off screen with your new function.

You also need to add a new function to the Laser script to allow for the boss to adjust the rotation of the laser. It will take a float as an input and then use that float to rotate the laser using transform.Rotate.

Next, return to the Boss script. It is time to do the initial setup for the boss attacks. First create four variables that will be used for the attacks. The first two will be floats to track the fire rate and if the boss can fire just like the enemy. Next add an int to track which weapon is the next to fire. Then add a GameObject for the Laser prefab, make it a SerializeField variable then assign it in the Unity editor.

Now create a function called FireWeapon. This will first contain the initial setup without the weapons actually firing. First check if Time has passed the can fire time. Then update the fire rate with a random range and the update the can fire time. This will use a switch statement for which weapon or attack it is using. For now add in the base of what will be used in the switch statement for case 0, 1, and the default. If you want to add more weapons or attacks later this setup will make it much quicker to do so. This setup will allow the weapons to fire every couple of seconds rotating through the different weapons.

Next, create a function called FireRotatedLaser that will instatiate and rotate the laser. Instantiate the laser at the Boss’ position. Make sure you assign it to a GameObject. Assign it as an enemy laser. Then rotate the laser. Pass the RotateLaser function you created earlier the rotation.

Now it is time to add in the weapons attacks. The first weapon will shoot a spread of lasers. First add three more global variables. A float to track laser rotation. An int to track the number of lasers to shoot. A float for the starting laser rotation. These will be used to set the rotation of each laser as it is fired.

Next, you will add the code for the spread weapon to the switch statement. Add a for loop to fire all the lasers. Set the rotation to starting laser rotation plus the laser rotation times the current laser in the for loop. Then call FireRotatedLaser passing the laser rotation. Make sure you set the current weapon to the next weapon.

The second weapon will fire 5 lasers at the player in rapid succession. First add a global int variable to track the number of targeted lasers to fire and set it to 5.

Next, you will add the code for the targeted laser weapon. First check if the number of lasers to fire has reached 0. If not Get the player direction Vector by subtracting the postion of the boss from the player position. Then use Vector3.Angle to get the player angle. The player angle returns as positive no matter which side of the boss the player is on so check if the player is on the left side by checking if the players x position is negative if it is make the player angle negative. Then call FireRotatedLaser passing it the player angle. Set the can fire time to Time.time + 0.2f. This will make the lasers fire in a quick burst. Then decrement the number of target lasers to fire variable. In the else for checking the number of target lasers to fire, set that number back to five and switch the weapon.

That will setup two weapons for the boss to attack with. Feel free to add more using the switch statement.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store