In this article, we will cover how to setup a generic Wave Spawner. I am attaching this Wave Spawner script to a Spawn Manager object, but if you have a Game Manager object you can attach to that as well.
In the Wave Spawner script, the first thing we will do is define what a wave is being creating a new public class. Inside this class there are going to be a few public variables:
- Wave Name — this will be use to show what wave the player is on and can be displayed using the UI.
- Enemy Container(s) — this is going to store the different enemy prefabs that will be spawned in each wave.
- Enemy Count — how many enemies will be spawn per wave.
- Spawn Rate — how quickly each enemy will be spawned.
Now that we have declared what makes up a Wave, we want to make accessible in the Inspector, so we will Serialized the class by using [System.Serializable].
When we create an array of the Wave class you will see how each element has those for variables we declared for in that Wave class.
We are going to declared a three more variables
- Next Wave — is the index of the wave that will be created next.
- Time Between Waves — will be the amount of time that will pass after one wave is complete and the next one will begin.
- Wave Countdown — is going to be used to display the time between waves using the UI.
In the On Start, we are going to set the wave count down to the time between waves.
In the Update, when the wave count down is less than or equal to 0 we want to start spawning the enemies of whatever wave we are on. We could create a method to do this, but could be problematic placing it in the Update because that method will be called each time the frame is drawn. Instead, we will use an enumeration that will represent the following states: Spawning, Waiting, Counting.
What is an Enumeration?
An Enumeration is a variable that is a set of constants. For example, we could use integers to represent the cardinal directions of a compass.
This can be difficult to remember what integer represents what direction and can make reading the code a bit difficult too.
We can declare an enum Direction and pass in our cardinal directions:North, East, South, West. So now when we use the enum North that is really saying 0 or East is really saying 1 and each time we are increasing the constant by 1. But, we are not done setting up the enum.
Now we will need to create a variable of the enums type. We we will set the enum Direction to be stored in a variable called myDirection.
We can now treat myDirection like any other variable but instead of a constant variable having a value of 10 or a float variable having a value of 10.99. The myDirection variable can have a value of Direction.North, Direction.East, Direction.South or Direction.West.
Back to the Wave Spawner
We create a enumeration called SpawnState with the different constants:
Spawning — will represent when enemies are still being spawned.
Waiting — will represent when the player has not defeat all of the enemies in a certain wave.
Counting — will represent when the wave countdown is in progress.
We need a variable to store the SpawnState enum, so we will call the variable state and initially set it to the Spawn State of Counting because when the game starts we want the first wave countdown to begin.
Finally, when the wave countdown gets to 0 we will check the state and if it not spawning we want it to start spawning and anything else we want the wave countdown to continue countdown constantly using Time.deltaTime.
This is it for part 1 of the Wave Spawner.