Upcoming Enemy Wave- Game Dev Series 42
Objective: create waves of enemy with limited spawning counts.
Compare with arcade shooter game with unlimited enemies, waves of enemy would make a section goal to players and also gives our game a proper ending.
To create waves of attack, we will need to adjust 2 scripts.
In SpawnManager, we will use wave coroutine instead of enemy spawning coroutine. To do so, we will need some variables.
We can set the wave counts in the inspector by _waveCounts. For example, if you set 5 then you should have 5 waves total.
_currentWave is what we use to count the current wave. It will increase when a wave ended, which we will add it in code later.
_enemySpawned is used to count how many enemies has already spawned.
To prevent any unpredictable error caused, we can set the value of _currentWave to 0 in Start().
With these 3 variables, we can create our enemy waves.
Create a new coroutine and set a while loop in it. Inside this loop, we will add the number of current wave, set the spawned enemy to 0.
To start spawning, we also need a bool turn on. Then use this bool as condition to start spawning in another while loop.
Inside this _isSpawning loop, we will use 3 parts as wave spawning routine.
- If the amounts of spawned enemy is less than the wave spawning limits, keep spawning every specific time.
- If the spawning of this wave ends, check if any enemy prefab left in the container. If none, wave ends and increase 1 to current wave count then set the _enemySpawned to 0.
- Spawning loop again.
Then we can use the original enemy spawning code as new method SpawnEnemy(). This is also where we increase the amount of _enemySpawned.
Our wave spawning is almost ready.
Last step is enable the bool to start the spawning in StartSpawning().
The next is our UI. We will make the UI to flash the current wave number at the beginning of every wave.
First, lets create a wave text in Canvas. And adjust the size and position.
And also create an animation with Canvas to animate the wave text.
Inside the Animator editor, create an empty state as default and set a trigger transition to wave text. Since our animation would stay for 5 seconds, set a transition back to default state.
Then inside UIManager, create 2 variable for, and also a public method to control it.
With this public method, we can back to SpawnManager() to call it.
Before we call the method, we also need to create a variable and debug it.
There are 2 location we need to call the wave text.
1 is at the first time we increase the current wave count.
2 is in the while loop after we eliminate all the enemies in a wave.
All done! Time to face the enemies wave by wave.
Next: Space Poison Incoming!