- Home /
Question by
weekywhawha · Apr 29, 2020 at 11:18 AM ·
c#script.unityeditor
Reset Timer when a Powerup is picked by the Player.
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);
}
}
}
}
Comment
Best Answer
Answer by weekywhawha · Apr 29, 2020 at 03:21 PM
by making theses changes.
public IEnumerator SpawnPowerup()
{
yield return new WaitForSeconds(powerupTimer);
Instantiate(powerupPrefab, spawnPointsPowerup[Random.Range(0, spawnPointsPowerup.Length)].transform.position, powerupPrefab.transform.rotation);
}
and then once this coroutine is public I can call it from my player script when there is a trigger
if (other.CompareTag("Powerup"))
{
hasPowerup = true;
Destroy(other.gameObject);
StartCoroutine(PowerupCountdownRoutine());
StartCoroutine(spawnManager.SpawnPowerup());
}