I’m trying to make a spawner spawn enemies faster over time. However, I’m having a little difficulty figuring out a repeating method that would speed up at a controlled rate. This was one of my attempts.
public GameObject prefab; //enemy that will spawn
public float startSpawn; //when the spawner will activate
//spawner management variables
public float spawnRate;
public float defaultRate;
public float timeReduction;
public float minRate;
//range for where enemy will spawn
public float minHeight;
public float maxHeight;
//spawns in enemy while reducing the time for the next enemy to spawn
public void Spawn()
{
GameObject enemy = Instantiate(prefab, transform.position, Quaternion.identity);
enemy.transform.position += Vector3.up * Random.Range(minHeight, maxHeight);
if (spawnRate > 1)
{
spawnRate -= timeReduction;
}
}
//when player hits start button, game manager enables spawner
public void OnEnable()
{
InvokeRepeating(nameof(Spawn), startSpawn, spawnRate);
}
//spawner is disabled when player dies
private void OnDisable()
{
spawnRate = defaultRate;
CancelInvoke(nameof(Spawn));
}
}
I’ve tried using IEnumerator/WaitForSeconds, but it ended up with enemies spawning uncontrollably because it wouldn’t pause my spawn method. I’m still every new to C# and unity, so I’m not sure where to go from here. Any advice would be great. Thank you!