Spawning bug problem
Okay basically i have a 2d game that spawns enemies from the left side and the right side of main camera's view. I have included a spawn delay so my enemies will come in after a certain time. I also have a difficulty factor set to "0.9". My problem is that after a long time of playing the game my spawner begins to bug. It ignores the spawn delay and spawns more and more enemies at a time (it's kind of creepy!)
If anyone can help me please do! Thank you!!!
Here's my script:
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;
public GameObject ObjectToSpawn;
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++)
{
// Random position within this transform
Vector3 rndPosWithin;
rndPosWithin = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), Random.Range(-1f, 1f));
rndPosWithin = transform.TransformPoint(rndPosWithin * .5f);
Instantiate(ObjectToSpawn, rndPosWithin, transform.rotation);
}
}
}
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());
}
}
I tried changing the m_DelayFactor = 1.0f; to m_DelayFactor = 5.00f; but it does the same but slowly, i also tried it like this: m_DelayFactor=0.1f; but it was larger than before the original (and creepier) Which I do not want! Thank you again, I appreciated it very much.
Answer by dkjunior · Oct 09, 2015 at 04:27 PM
With each wave it multiplies the delay factor by difficultyFactor, so as long as difficultyFactor is less than 1.0, the delay factor will be getting smaller and smaller eventually diminishing to zero. That would result in behavior that you observe (spawning more and more objects with literally no delay). I thought that was the point of difficulty factor (the game gets more difficult over time).
If you want your enemies to be spawned at a constant rate defined by difficultyFactor, move the m_delayFactor assignment to the beginning of the coroutine:
IEnumerator SpawnLoop()
{
m_DelayFactor = difficultyFactor;
...
}
Thank you for answering my question but I'm confused could you please explain a bit more? Thank you once again @dkjunior
With your code above, the delay factor gets smaller with each wave:
Wave 1: m_delayFactor = 1.0 Wave 2: m_delayFactor = 1.0 x 0.9 = 0.9 Wave 3: m_delayFactor = 1.0 x 0.9 x 0.9 = 0.81 ... and so on.
Eventually it will be zero, resulting in enemies spawning without delay. This is what you observe after playing the game for some time.
What is the desired behavior? If you want the game to get faster with time, then your code is actually correct. You may just want to tweak the difficulty factor (say, change it 0.95) if it's too fast.
If you want your enemies to be spawned at a constant rate, move the m_delayFactor *= difficultyFactor; line out of the main loop so that it doesn't change with each wave.
Hope this helps.
Thank you for answering back so quick. I don't really want a difficulty factor in my script but when i took it out and saved it it doesn't spawn anything!! i understand what you mean know on which i am very grateful for that, but I just want it to spawn normally each wave. Thank you once again!! @dkjunior
If you just want it to spawn normally each wave, keep your original code and set difficulty factor to 1. Glad I was able to help. Please accept the answer if it addressed your need :)