Quantcast
Channel: Questions in topic: "invokerepeating"
Viewing all articles
Browse latest Browse all 220

Making a spawner adjust its spawnrate over time

$
0
0
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!

Viewing all articles
Browse latest Browse all 220

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>