You've already forked zumbi-game
randomize delay between zombie spawns
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user