Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Reset Timer when a Powerup is picked (Like Quake)

Discussion in 'Scripting' started by weekywhawha, Apr 28, 2020.

  1. weekywhawha

    weekywhawha

    Joined:
    Jan 2, 2020
    Posts:
    4
    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).

    Code (CSharp):
    1.     IEnumerator SpawnPowerup()
    2.     {
    3.         while (true)
    4.         {
    5.             yield return new WaitForSeconds(powerupTimer);
    6.             if (GameObject.FindGameObjectsWithTag("Powerup").Length == 0)
    7.             {
    8.                 Instantiate(powerupPrefab, spawnPointsPowerup[Random.Range(0, spawnPointsPowerup.Length)].transform.position, powerupPrefab.transform.rotation);
    9.             }
    10.  
    11.         }
    12.     }
    Adding the full code to make it more clear

    Code (CSharp):
    1. public class SpawnManagerHandler : MonoBehaviour
    2. {
    3.     public GameObject[] enemyPrefabs;
    4.     public GameObject[] spawnPointsEnemies;
    5.     public GameObject powerupPrefab;
    6.     public GameObject[] spawnPointsPowerup;
    7.  
    8.     private float maxWait = 2f;
    9.     private float minWait = 0.5f;
    10.     private float randTimer;
    11.  
    12.     private int powerupTimer = 10;
    13.  
    14.     private int randIndex;
    15.  
    16.  
    17.     private void Start()
    18.     {
    19.         StartCoroutine(SpawnTimerEnemies());
    20.         StartCoroutine(SpawnPowerup());
    21.     }
    22.  
    23.     IEnumerator SpawnTimerEnemies()
    24.     {
    25.         SetRandomValues();
    26.         int enemyIndex = Random.Range(0, enemyPrefabs.Length);
    27.         yield return new WaitForSeconds(randTimer);
    28.         Instantiate(enemyPrefabs[enemyIndex], spawnPointsEnemies[randIndex].transform.position, enemyPrefabs[enemyIndex].transform.rotation);
    29.         StartCoroutine(SpawnTimerEnemies());
    30.  
    31.     }
    32.     private void SetRandomValues()
    33.     {
    34.         randTimer = Random.Range(minWait, maxWait);
    35.         randIndex = Random.Range(0, spawnPointsEnemies.Length);
    36.     }
    37.     IEnumerator SpawnPowerup()
    38.     {
    39.         while (true)
    40.         {
    41.             yield return new WaitForSeconds(powerupTimer);
    42.             if (GameObject.FindGameObjectsWithTag("Powerup").Length == 0)
    43.             {
    44.                 Instantiate(powerupPrefab, spawnPointsPowerup[Random.Range(0, spawnPointsPowerup.Length)].transform.position, powerupPrefab.transform.rotation);
    45.             }
    46.  
    47.         }
    48.     }
    49. }
     
    Last edited: Apr 28, 2020
  2. sbalanoff

    sbalanoff

    Joined:
    Nov 14, 2019
    Posts:
    36
    Instead of using while(true) use a private boolean, just makes it easier for later and you won't have an infinite loop. You are also going to want to reset powerupTimer and randTimer in SetRandomValues.
     
  3. weekywhawha

    weekywhawha

    Joined:
    Jan 2, 2020
    Posts:
    4
    Thank you! Got it to work :)