Spawning the Boss

This tutorial will cover adding the Boss to the Spawn Manager. Start by going to the SpawnManager script. To start you will need to add 3 global variables. The first is a GameObject for the boss prefab. Make sure you make this a serialized field and assign it in the Unity Editor. The second is a bool the will tell the spawn manager if it is on a boss level or not. The third is an int that will keep track of how many boss’ have been defeated. This will be used to increase the health of the boss if multiple boss levels are reached.

[SerializeField] private GameObject _bossPrefab;
private bool _bossLevel = false;
private int _bossCount = 0;

Next, wrap the if and else statement inside the enemy spawn function inside another if statement to check if it is a boss level or not.

IEnumerator SpawnEnemyRoutine()
{
while (_stopSpawning == false)
{
if (_bossLevel == false)
{
... // Enemy spawning
}
yield return new WaitForSeconds(_waitTime);
}
}

Next, update the if statement that checks if all enemies are dead and starts spawning a new wave. The updates you want to make start at the bottom of this section. Add an if statement to check if the wave number subtracting 1 the taking mod 4 of it and check if it is equal to 0. Include a check that the wave number is not 1 so you don’t spawn a boss on the first level. Inside the if statement set that it is a boss level. Then spawn the boss to the top of the screen. Set the bosses health. You will create the function in a couple of steps. Then update the wave in the UI Manager to show “Boss”. If the UpdateWave function in the UI Manager isn’t set to handle strings update it to do so.

if (_numEnemies == 0)
{
_waveNumber++;
_numEnemies = _waveNumber * 2;
_numEnemiesToSpawn = _numEnemies;
_moveType = (_waveNumber - 1) % 4;
_shieldTrigger = ((_waveNumber - 1) % 12) / 4;
_uiManager.UpdateWave(_waveNumber.ToString());
if (_waveNumber != 1 && (_waveNumber - 1) % 4 == 0)
{
_bossLevel = true;
GameObject bossSpawn = Instantiate(_bossPrefab,
new Vector3(0f, 12f, 0f), Quaternion.identity);
Boss boss = bossSpawn.GetComponent<Boss>();
boss.SetBossHealth(_bossCount);
_uiManager.UpdateWave("Boss");
}
}

The last thing to do in the SpawnManager script is to create a OnBossDeath function that is public. In the functions set the _bossLevel variable to false. Increment the boss defeated count. Then update the wave in the UI to the correct number.

public void OnBossDeath()
{
_bossLevel = false;
_bossCount++;
_uiManager.UpdateWave(_waveNumber.ToString());
}

Now go to the Boss script. First add a global variable for the SpawnManager. Make sure you assign it in the Start function.

private SpawnManager _spawnManager;

Next, create the SetBossHealth function that was talked about before. The function should take an int. Inside the function set the health of the boss to 10 plus 5 times the int that is passed to the function.

public void SetBossHealth(int multiplier)
{
_health = 10 + (5 * multiplier);
}

The last thing to do is to call the OnBossDeath function inside the TakeDamage function before the boss is destroyed.

_spawnManager.OnBossDeath();

Now the boss will spawn after four rounds of enemies.

-Chris

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

My Google Code-in 2018 experience as a winner

Roadmap to Becoming a Web Developer

Rest Assured Tutorial | Day 1 | Client Server Architecture Explained (API Testing Basics)

What is ECS?

Positional-Only Arguments in Python

Could Transpilers Help Corporations Retain Talent?

Five steps become masters of web scraping in selenium(project walmart.com)

How to easily scrap numerical data? (or find an API endpoint)

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
Christian Carter

Christian Carter

More from Medium

Adding a health powerup to our prototype

Galaxy Shooter — Powerup Magnet

Light layers in Unity HDRP

Loading Scenes In Unity