Wave of Enemies Incoming!
Objective: Create a wave system for our player to fight through.
In the current state, the Game starts with the player shooting an asteroid, and the asteroid starts the spawn manager. The spawn manager creates enemies and powerups every 2.5 seconds until the player dies. This is great as a prototype, however if we want this to feel like a real game then we should create a wave system.
The desired effect is to have a message appear indicating enemy’s are approaching and then a set amount of enemies spawn and then the spawn manager waits until the enemies are destroyed and a new wave of enemies can begin.
To begin, as always, we need to create everything from scratch. On the spawn manager we need to create an int variable for the wave count, an int variable for the number of enemies to spawn, and int for the number of enemies destroyed.
We need to create is a UI text element that says, “Incoming Wave.” and a text element that tells the player what wave they are on.
Set the Text for incoming wave to not active
Now create a method on the UI manager script to that will set the incoming wave text to active. After that we want the method to start the coroutines on the spawn manager to start spawning. We will set the asteroid to call this method when it’s destroyed.
We also need to create a coroutine to wait after 2.5 seconds then turn the incoming wave text to not active.
Now on the spawn manager we need to address the methods being called by the incoming wave. First, we need to have one method that starts the coroutines for the powerups.
The other method we want the incoming wave to call is going to handle the wave number as well as resetting the variable for enemies destroyed, call the method that sets the number of enemies to spawn, and starts the spawning coroutine for enemies.
The method for setting the number of enemies to spawn will consist of a switch statement where the wave count variable will determine the enemy spawn number variable. Meaning if the wave count is 3 then the amount of enemies to spawn is 4.
Now the enemy spawn coroutine we get rid of the while loop and replace it with a for loop. First set a yield for time you have the incoming wave text active. This way when enemies will wait until the incoming wave is gone before they begin to appear. In the for loop, we have set the limiter to the variable that represents the enemy spawn amount. When the for loop is done, we want to stop the coroutine. I used this to stop a bug I was encountering where the spawn amount was getting duplicated when the coroutine was called with each wave.
Now we need to create a method that handles what to do when all the enemies are destroyed. We need to have the enemy destroyed variable have 1 added to it when this method is called. We need to use an if statement for when the number of enemies destroyed is equal to the number of enemies spawned then it calls a method on the UI manager that creates a new wave.
The new wave method will activate the incoming wave text, call the method on the spawn manager that set the wave count as before, call the method that changes the wave number UI text, and start the coroutine that turns the incoming wave text off.
Now we need to open the enemy text and navigate to the method that handles the destruction of the enemy. Inside we need to have that method call the enemy destroyed method on the spawn manager.
We need to have the wave number be displayed for the player and we have it being called every time the incoming wave or the new wave method is called. To have the text update we use this method.
This is all there is to setting up a basic wave system. You can set the values to what better suits your needs. As always, don’t be afraid to experiment with your code and I’ll see you in the next tutorial.