I am trying to make spawnpoints where the zombies spawn and keep spawning like a wave system I looked up a basic way to spawn enemies and I’m running into some issues.
Right now I am only able to spawn zombies once and when they are dead no more spawn
The way the code is set up it makes its very hard to implement for eg. I have to mark the range of x, y and z positions soo zombies can only spawn in that particular area.
As my level has elevations because of how I have set up my terrain if the Y values are not set perfectly the zombies spawn in and either have feet buried in the ground or floating in the air
What exactly is your idea? How are the zombies supposed to spawn? What are your rules?
From what I understood, you managed to spawn the first wave but not additional zombies. Given I understood that correctly, you could parent the zombies to game objects and check the childCount of those game objects in the Update method or in the SpawnEnemy method. Depending on the childCount, you could spawn new zombies. No need to use the enemyCount variable and take care of dead zombies.
Do not call StartCoroutine in Update. This starts a new coroutine each frame. With a framerate of 60, 60 new coroutines would get started per second. That’s a waste of resources and decreases the performance of your game significantly. Have the coroutine run the entire time or drop the coroutine and write a timer for your Update method.
One more thing you could do to improve the performance of your code: If you use a coroutine and if the value of spawnDelay does not change, cache the WaitForSeconds object instead of creating a new one in each iteration step of the while-loop.
I tried making a new script new a new system where I’m using enums for Spawning Waiting and Counting. Im updating the coroutine to cycle wave after wave. But I got stuck at another place where my script is suppose to count down from 5 secs and as it hits 0 the wave should start spawning but on clicking play the count doest go down on its own and I have to manually set it to 0 for it to start work. But after that it loops through all the states and counts down to negative value which it shouldn’t be doing.
Your question is why waveCountDown does not get set to 0 or an initial value? That’s because the value gets set in Start() but then, only waveCountDown -= Time.deltaTime; gets executed.
After the zombies were spawned (or whatever your idea is), you have to execute waveCountDown= timeBetweenWaves; to reset your timer. If I understood your code correctly, I would expect this line in the SpawnWave method where you set state = SpawnState.WAITING;.
Ohk After a whole day of head scratching I see where I went wrong I made a silly mistake where i forgot to add a “{}” which changed the logic of the method and I didn’t see it as I did not get any errors in my script.
Mistake in Update-
if (waveCountDown <= 0)
{
if (state != SpawnState.SPAWNING)
{
StartCoroutine(SpawnWave(waves[nextWave]));
}
{ - // not having this bracket caused problems
else
{
waveCountDown -= Time.deltaTime;
}
}
Here is a little improvement in case you are interested:
WaitForSeconds delay = new WaitForSeconds(1f / _wave.rate);
for (int i = 0; i < _wave.count; i++)
{
SpawnEnemy(_wave.enemy);
yield return delay ;
}
Depending on the value of _wave.count, caching the WaitForSeconds object could have a significant positive impact on the performance of your code because the program won’t create x WaitForSeconds objects but reuse the one that has already been created.