randomize delay between zombie spawns

This commit is contained in:
2026-05-05 11:43:15 +02:00
parent 38b1456c7e
commit 1e35195ab8
2 changed files with 13 additions and 3 deletions
+12 -2
View File
@@ -4,15 +4,18 @@ using UnityEngine;
public class ZombieSpawnerLogic : MonoBehaviour
{
public GameObject Prefab;
public byte CooldownSeconds = 5;
public byte MinCooldownSeconds = 5;
public byte MaxCooldownSeconds = 30;
public byte MaxSpawns = 5;
private float _currentSpawnDelay;
private float _lastSpawn;
private readonly List<GameObject> _spawns = new();
void Start()
{
_lastSpawn = Time.time;
PickNewSpawnDelay();
}
void FixedUpdate()
@@ -21,9 +24,15 @@ public class ZombieSpawnerLogic : MonoBehaviour
AttemptZombieSpawn();
}
private void PickNewSpawnDelay()
{
_currentSpawnDelay = Random.Range(MinCooldownSeconds, MaxCooldownSeconds);
Debug.Log($"Zombie spawner new spawn delay: {_currentSpawnDelay}s");
}
private void AttemptZombieSpawn()
{
if (Time.time - _lastSpawn < CooldownSeconds)
if (Time.time - _lastSpawn < _currentSpawnDelay)
{
return;
}
@@ -38,6 +47,7 @@ public class ZombieSpawnerLogic : MonoBehaviour
var newZombie = Instantiate(Prefab, transform);
_spawns.Add(newZombie);
_lastSpawn = Time.time;
PickNewSpawnDelay();
}
private void RemoveDestroyedZombies() => _spawns.RemoveAll(zombie => zombie == null);