Boss Attacks

Christian Carter
4 min readSep 22, 2021

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.

void CheckOffScreen()
{
if (transform.position.y <= -8.0f || transform.position.y >= 9.0f
|| transform.position.x <= -11.3f
|| transform.position.x >= 11.3f)
{
if (transform.parent != null)
{
Destroy(transform.parent.gameObject);
}
Destroy(this.gameObject);
}
}

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.

public void RotateLaser(float rotation)
{
transform.Rotate(Vector3.forward * rotation);
}

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.

private float _fireRate = 2.0f;
private float _canFire = 2.0f;
private int _currentWeapon = 0;
[SerializeField] private GameObject _laserPrefab;

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.

void FireWeapons()
{
if (Time.time > _canFire)
{
_fireRate = Random.Range(2.0f, 3.0f);
_canFire = Time.time + _fireRate;

switch (_currentWeapon)
{
case 0:
_currentWeapon = 1;
break;
case 1:
_currentWeapon = 0;
break;
default:
_currentWeapon = 0;
break;
}
}
}

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.

void FireRotatedLaser(float rotation)
{
GameObject laser = Instantiate(_laserPrefab, transform.position,
Quaternion.identity);
Laser bossLaser = laser.GetComponent<Laser>();
bossLaser.AssignEnemyLaser(1);
bossLaser.RotateLaser(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.

private float _laserRotation = 30f;
private int _numSpreadLasers = 7;
private float _startLaserRotation = -90f;

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.

case 0:
for (int i = 0; i < _numSpreadLasers; i++)
{
float laserRotation = _startLaserRotation +
(_laserRotation * i);
FireRotatedLaser(laserRotation);
}
_currentWeapon = 1;
break;

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.

private int _numTargetLasers = 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.

case 1:
if (_numTargetLasers > 0)
{
Vector3 playerDirectionVector = _player.transform.position -
transform.position;
float playerAngle = Vector3.Angle(playerDirectionVector,
Vector3.down);
if (_player.transform.position.x < 0)
{
playerAngle = playerAngle * -1;
}
FireRotatedLaser(playerAngle);
_canFire = Time.time + 0.2f;
_numTargetLasers--;
}
else
{
_numTargetLasers = 5;
_currentWeapon = 0;
}
break;

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

-Chris

--

--