- Home /
Enemy Wave Generator Spawn System Help!
Am developing a spaceShooter(3D Game) and got in to a wall. Am trying to create a enemy Spawn waves which will spawn 3 different types of enemies as listed below, infinitely. There will be 4 waves, each wave will spawn enemy prefabs in a timely manner. Once all the four waves get completed again it get looped so basically it will be infinite 4 waves and speed of spawn increases after each 4 waves.
Remember please am in a learning curve, I need an efficient way to structure the below logic.. I knew it is not an efficient manner and would like to structure the concept in an efficient manner. I do have done lot of research and found many wave generators most of which is not similar to the one am looking for... So, please help me out here. Thanks a lot for your time and Happy Coding!
enemy prefabs
basicEnemyShip prefab
mediumEnemyShip Prefab
Boss EnemyShip Prefab
Waves of enemies
First Wave
Second Wave
Third Wave
Fourth Wave
First Wave
{
Timer 20 Sec
Instantiate basic enemy prefab(N Numbers)
Timer 25 Sec
Instantiate mediumEnemyShip (N Numbers)
Timer 30 Sec
Instantiate BossEnemyShip Prefab
}
Second Wave
{
Timer 30 Sec
Instantiate basic enemy prefab (N numbers)
Timer 35 Sec
Instantiate Medium EnemyShip (N Numbers)
Timer 30 Sec
Instantiate BossEnemyShip Prefab
}
Third Wave
{
Timer 40 Second
Instantiate basic enemy prefab + mediumEnemyShip (N numbers Random Numbers)
Timer 30 Second
Instantiate bossEnemyShip Prefab
}
Fourth Waves
{
Timer 50 Second
Instantiate mediumEnemyShip+BossEnemyShip
Timer 40 Second
Instantiate BossEnemyShip
}
Wave loops
{
First Wave
Second Wave
Third Wave
Fourth Wave
}
Enemy spawn Amount increases end of each loops (4 waves)
enemy speed increases end of each loops (4 waves)
Have you tried unity's tutorial project Space Shooter?
Thanks for your message. Yeah! I did tried rohitanshu, I need an efficient structure to loop this using coroutine. Problem is in that tutorial it is just some basic Ienumarator explained. But I need an proper structure for my logic. Hope I make a sense here.
Yeah, you do. ;) Then I think Bunny83's answer would suffice. Of course, you should read it, learn it and tweak it according to your needs. All the best. :)
Answer by Bunny83 · Jun 26, 2014 at 09:30 AM
I just had nothing else to do, so i wrote a simple wave generator ;)
// C#
// WaveGenerator.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class WaveAction
{
public string name;
public float delay;
public Transform prefab;
public int spawnCount;
public string message;
}
[System.Serializable]
public class Wave
{
public string name;
public List<WaveAction> actions;
}
public class WaveGenerator : MonoBehaviour
{
public float difficultyFactor = 0.9f;
public List<Wave> waves;
private Wave m_CurrentWave;
public Wave CurrentWave { get {return m_CurrentWave;} }
private float m_DelayFactor = 1.0f;
IEnumerator SpawnLoop()
{
m_DelayFactor = 1.0f;
while(true)
{
foreach(Wave W in waves)
{
m_CurrentWave = W;
foreach(WaveAction A in W.actions)
{
if(A.delay > 0)
yield return new WaitForSeconds(A.delay * m_DelayFactor);
if (A.message != "")
{
// TODO: print ingame message
}
if (A.prefab != null && A.spawnCount > 0)
{
for(int i = 0; i < A.spawnCount; i++)
{
// TODO: instantiate A.prefab
}
}
}
yield return null; // prevents crash if all delays are 0
}
m_DelayFactor *= difficultyFactor;
yield return null; // prevents crash if all delays are 0
}
}
void Start()
{
StartCoroutine(SpawnLoop());
}
}
This allows you to define as many waves you like and each wave can have mutiple spawn actions each with a custom delay. You can also spawn multiple different prefabs at the same time by setting the delay of subsequent actions to 0. You can also use this to just print a message by not assigning a prefab but only a message.
It's a quite "hacky" solution but quite versatile and easy to extend.
Here's how it looks like in the inspector where you would define your waves:
edit
I forgot that you wanted to lower the delay each iteration. I've added a simple factor which is lowered each iteration by 10% (times 0.9). So during the first iteration the factor is 1.0 during the next iterations it will be 0.9 | 0.81 | 0.729 | ... always 10% of the previous iteration
ps: when editing the waves keep in $$anonymous$$d that you can duplicate or delete array elements in the inspector. That simplifies editing since you can duplicate whole waves.
Bunny! Thanks a million...! Highly Appreciated!. I, never expected someone would be this helpful.I will check it now and will let you know (If I need any explanation from this code and how it works!) Thanks a lot.
Since am in a learning curve this system looks quiet complicated. However I do have a question (might be a stupid question! :))
Is it possible to add my own prefab example like basicEnemyPrefab declare it and then can instantiate it inside the loop that you have created? can you please write an example code so I would get the idea. Just for one object if you can explain for one prefab I would get the idea! Thanks again sorry for shooting with multiple questions!
Uhm that's the purpose of the whole thing ;) You have to assign your prefabs in the inspector for each "spawn action" to the corresponding "prefab" variable. I haven't included the actual instantiate code since that depends on your map / scene.
There are two "TODOs" in the code. One is the instantiation of the prefab, the other is the ingame message. If you don't need the message you can simply ignore that ;)
I can highlight where you would assign your prefabs in the image, just a second..
edit
I added the lowering of the spawn delay each iteration.
@NEWBIE_1 : Yes it can be. It's actually structured for a 3D game (see the question, plus how Vector3 is used). But you'd have to tweak it somewhat according to you specific needs.
Your answer
![](https://koobas.hobune.stream/wayback/20220613152534im_/https://answers.unity.com/themes/thub/images/avi.jpg)