Implementing an Enemy Waves System in my Game

Michael Lam
3 min readSep 19, 2023

--

Today, I added an enemy waves system to my game and BOYYY was it more difficult than I thought. In total, I was probably stuck on this implementation for about 2 hours trying to wrap my brain around how I’m going to do this and have it work properly. Here are the SparkNotes:

I first created two UI Texts: one to display what wave the player is currently on and the other to display how many enemies left that needed to be destroyed before moving on to the next wave.

Inside the Asteroid script to display “Wave 1” once the asteroid has been hit by the laser

I added them to the UI Script and created three functions that will utilize these two UI text fields. The first one adjusts the enemies remaining, the second one to update the waves text, and the third one to simulate the appearing and disappearing of the wave text.

The Spawn Manager script was where I decided to put all of this code (there might have been a better way or place to put all of this wave code but, oh well). I hardest part for me was determining which and how many variables I need to make this work. I ultimately decided on these variables above.

  • _enemySpawnRate: is kind of self explanatory
  • _waveNum: is the number of the current wave
  • _enemyNum: is the total number of enemies that need to be destroyed for the wave to end
  • _enemyRemaining: is the number of enemies left to be destroyed to end the wave.
On the Enemy Script when they die.
On the Spawn Manager Script

Then, I created 2 new functions and adjusted the spawn routine to incorporate the waves system.

The first function is one that will be called from the enemy script when the enemy dies and it will reduce the enemy remaining variable and adjust the UI accordingly.

The second function is called when the conditions for the previous wave have been fulfilled. It will start the next wave and adjust the UI accordingly.

The third function was originally just the enemy spawn routine but I’ve adjusted it to incorporate the waves system. I set the _enemyNum to 5 times the wave number, this makes each wave modular and adjust itself rather than having to specifically type out how many enemies are in each wave. I set the starting value of _enemyRemaining to _enemyNum so that they match at the start of every round and adjust the UI accordingly.

Inside the while loop, I made the condition that while the number of enemies remaining is greater than zero, enemies will continue spawning. Once it hits 0, the enemies will stop spawning. Then, once the enemies stop spawning, the spawn manager will destroy any existing enemies (so theres no left over) and start the next wave.

After 2 hours of headaches, I’m happy to say that my enemy wave system finally works!!

--

--