Help with Spawning Waves.

Hello. :slight_smile:

I’m trying to make a Wave Spawner for a simple game, and i need some help with controlling the Waves.
I searched for a solution for this but couldn’t find one that does what i’m looking for…
What i’m trying to do is:
When the Level starts i want to start a Countdown (Maybe a Coroutine?) before starting the first Wave.
After the first Wave finished, start another Countdown for the next Wave (If there is one) an so on.
Each Wave[ ] will have a set amount of enemies to Spawn, a Timer between spawns and the actual Enemies,
wich is a array of GameObjects that will be randomly picked and Instantiated at a random Spawn Point.
Something like the code below:

public class WaveSpawner : MonoBehaviour {

//This coltrols the current state of the WaveSpawner.
public enum WaveState {Waiting, Spawning, Finished};

//The actual Wave Class.
public class Wave {
public int EnemiesPerWave; //The amount of enemies the current Wave will spawn.
public float TimeBetweenEnemies; //The time in seconds between enemy spawns.
public GameObject[] Enemies; //The Enemies that will be spawned.
//Pick one like this?: GameObject randomEnemy = Random.Range(0, Waves[waveIndex].Enemies.Lenght);
}

public WaveState State; //Control the current state of the Waves.
public Wave[] Waves; //Class to hold Wave information. Increase it to add more Waves to the game.
public Transform[] SpawnPoints; //The random location the Enemy will spawn.
//Below is the current Wave the player are in: Waves[waveIndex].
public int waveIndex; //Increase this when player kills all EnemiesPerWave in current Wave.
private int spawnedEnemies; //This is to keep track of the amount of enemies spawned in current Wave.

void Update()
{
switch (State){ //Is it a good idea to make a switch to control the Waves?
case State.Waiting:
//Start the countdown. When it ends it will start a new Wave IF there is another Wave to spawn.
break;
case State.Spawning:
//Start spawning Waves.
break;
case State.Finished: //Triggered when player defeats all Waves.
//Wins the Level.
break;
}
}
}//

Thanks in advance! :slight_smile:

You can construct such a thing a bunch of ways. It is always some kind of balance between a coroutine (or a bunch of timer/counters) to trigger each step of a spawn, and a bunch of data objects (ScriptableObjects perhaps?) that define what happens when.

More importantly, keep in mind what it will need to do ultimately. Here are some possible requirements you might need. Thinking of them in advance can help you create a good solution.

  1. what happens when the player dies mid-wave? Does the wave restart? Does the wave spawner have to “pause” its production of spawns briefly and then resume it?

  2. what happens for really fast or really slow players? Does the spawner mindlessly spoo out enemies once every X seconds, or does it judge how you’re doing and “keep you busy?” If a player is very fast, they might be waiting for new enemies to appear. If a player is slow, they might get overwhelmed because they can’t clear things out fast enough.

  3. for any spawned “thingy,” what about it is parameterized? Let’s say you’re spawning zombies. How does the wave controller define and apply specific characteristics of zombies on later levels vs early levels? More of the zombies? Faster speed? Different actual zombie prefabs that behave differently?

  4. Does your game have any narrative breaks, such as “halfway through level 3 a special enemy comes out, does a taunting animation, says some things to the player, then scoots off.” If so, maybe you want other things to happen to the player, like suspend their inputs briefly, or perhaps disable their weapons while the cutscene is in progress.

  5. how tightly-coupled is your scene to the spawning process? Does the spawn process have to study the scene and find spawn locations before it can proceed? Can that code be common?

Personally I’ve settled mostly on having a special “wave spawner scene” that I load in additively on top of the main gameplay scene. This way I just have separate wave spawner scenes, configurable by global variables like “what wave am I on” and “what difficulty level am I playing at.” Basically my solution is a combination of code and data, as yours will be. I just choose to package it up in scenes since Unity works so well with scene editing.

1 Like

Thanks a lot for the reply @Kurt-Dekker . I’m a begginer in C# and this game is very simple mechanic and art wise. It’s main feature is the Wave Spawner.

Yes, if the player dies the level will restart.

The Enemies in the current Wave will be spawned using the Wave SpawnRate from the Wave Class and each Wave will be faster or slower.

I made a very simple Enemy A.I for this game that have some parameters like it’s graphics, animations, speed, health, damage, projectiles, etc.

No, it won’t have narratives, althrough it will have a Boss Wave, if i can manage to do it with some help.

Yes, the Enemies in each Wave will be spawned at random Spawn Points, maybe in a Coroutine SpawnEnemy?

Thanks again for the reply. :slight_smile:
I would be glad If you or someone else could help me manage the code to work.