Wave Spawner: Part 4
In Part 3, we finished up with the Spawning Sate of Waiting. We are going to finish up this Wave Spawner in this article.
The first thing we will do is create a WaveCompleted method and call it when we do the Enemy Alive check comes back false (signaling all of the enemies in that wave have been destroyed).
In the WaveCompleted method, we want to set the SpawnState to Counting to enter into the countdown of when the next wave will begin. We rest the wave countdown to the time between waves, which we declared to be 5 seconds.
We will want to perform a check to see if we have reached the last wave before incrementing the wave count and we do that by adding 1 onto the nextWave, which looks at the two waves ahead and if that is greater than the length of the waves array than we have reached the end. So, we will just reset the nextWave count back to 0 for now, but we can add a Game Over scene or a final Boss Fight.
Now it is time to start spawning the enemies and we will do this by creating a public array of Transform positions that will hold the different spawn points are enemies can be instantiated at.
We want it to select a random spawn point, so we will create a local variable that will select one of the spawn points in the array.
From here we can instantiate the enemy at the randomly selected spawn point position. Now you just need to add empty game objects that will be the spawn points and assignment in the Inspector. This is a working Wave Spawner and I did mention this is the last article on the wave spawner but I need to add text in the UI announcing the next wave and I want to be able to assign multiple kinds of enemies inn each way and as of now I can only assign one kind of enemy.
When testing I found a bug that was causing the wave countdown to never….countdown. I placed the Else inside the IF(waveCountdown ≤ 0) instead of it being and IF ELSE statement.