Phase II: Enemy Waves

Brian Branch
3 min readJun 9, 2021

--

Objective: Implement wave sequencing of enemies with more enemies coming in each wave.

As stated above, the latest added feature will have the enemies spawn in waves. There will be a short delay between waves and an on-screen display announcing the next wave.

First, I created a few more variables which I will explain.

Variables for Enemy Wave System
  • _waveCount: Keeps track of the current wave, which will start at 1.
  • _wavemax: Will set the maximum number of total waves.
  • _enemiesInWave: Holds the number of enemies to be spawned in the wave.
  • _enemiesSpawned: Tracks the number of enemies that have been spawned in the wave.
  • _enemiesAlive: Tracks how many enemies are currently alive.
  • _uiManager: used to get a reference to the UIManager for on-screen display of each wave.

In the Start() method, I get a reference to the UIManager and do a null check.

I also created three new methods.

  • WaveControl(): Which checks if _waveMax has been reached. If so, it ends all coroutines to end the waves. If not, it increments relevant values and calls the NextWave() coroutine. This will be called from the Update() method to keep track continually.
WaveControl() method.
  • NextWave(): Which has a 5-second pause then starts the coroutine SpawnEnemy().
NextWave() coroutine
  • EnemyKilled(): I made this a public method that will be called from the enemy when it is destroyed to keep track of how many enemies are still alive.
EnemyKilled() method

Next, I made some updates to the SpawnEnemy() coroutine.

SpawnEnemy() coroutine
  • I added a call to the _uiManager to update the wave display with the current _waveCount.
  • In the while loop, I added a check to run while the _enemiesSpawned is less than _enemiesInWave and while the player is still alive.
  • In the body of the while loop, after instantiating the enemy, I increment both _enemiesSpawned and _enemiesAlive by 1.

Then I created another text element and a coroutine in the UIManager to display the current wave and a public method to call the coroutine.

UpdateWave() method and WaveDisplay() coroutine in the UIManager.

The last thing I added was a call from the enemy when they are destroyed to the EnemyKilled() method in the Spawnmanager.

EnemyDestroyed() method in EnemyBehaviour script

This is how I made a basic wave system for the game. The way I made it, the designer can easily adjust values in the inspector to increase the number of waves and increase the number of enemies in the waves.

I hope this was interesting, and until next time I wish you well on your coding journey.

--

--