Hello,
I am not sure if I am doing this the correct way, I have a coroutine to spawn my powerup only when there is none on the map, what I am failing to achieve is to make the timer reset when a powerup is picked by the player (make it work a bit like a quake game, where once it’s picked up it will take fixed seconds to spawn again).
public class SpawnManagerHandler : MonoBehaviour
{
public GameObject[] enemyPrefabs;
public GameObject[] spawnPointsEnemies;
public GameObject powerupPrefab;
public GameObject[] spawnPointsPowerup;
private float maxWait = 2f;
private float minWait = 0.5f;
private float randTimer;
private int powerupTimer = 10;
private int randIndex;
private void Start()
{
StartCoroutine(SpawnTimerEnemies());
StartCoroutine(SpawnPowerup());
}
IEnumerator SpawnTimerEnemies()
{
SetRandomValues();
int enemyIndex = Random.Range(0, enemyPrefabs.Length);
yield return new WaitForSeconds(randTimer);
Instantiate(enemyPrefabs[enemyIndex], spawnPointsEnemies[randIndex].transform.position, enemyPrefabs[enemyIndex].transform.rotation);
StartCoroutine(SpawnTimerEnemies());
}
private void SetRandomValues()
{
randTimer = Random.Range(minWait, maxWait);
randIndex = Random.Range(0, spawnPointsEnemies.Length);
}
IEnumerator SpawnPowerup()
{
while (true)
{
yield return new WaitForSeconds(powerupTimer);
if (GameObject.FindGameObjectsWithTag("Powerup").Length == 0)
{
Instantiate(powerupPrefab, spawnPointsPowerup[Random.Range(0, spawnPointsPowerup.Length)].transform.position, powerupPrefab.transform.rotation);
}
}
}
}