Hey guys!
I have a WaveSpawner but in my case in one wave all enemies are spawned at the same time but actually I want them to spawn one by one so with kind of a delay. Maybe I could use "WaitForSeconds" but I don't know where. Also I would like to destroy them if they collide with another object but they just keep spawning. Hopefully you can help me out :)
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 actions;
}
public class WaveGenerator : MonoBehaviour
{
public float difficultyFactor = 0.9f;
public List 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());
}
}
↧