Search Unity

Increasing spawn enemies rate as times goes on.

Discussion in 'Scripting' started by BrokenhearT, Jul 3, 2015.

  1. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    I looked a lot on here, and for tutorials on youtube couldn't actually find anything about how can I make the enemies spawner increase the spawning rate over time, so if there's anyone would like to help really would appreciate it,
    I want the enemies to spawn the first 2 mins of the run time by 1 enemy every 3 seconds, then the time of course will go down to 2.9 after 2 mins, then another 2 mins will be 2.8 till it gets down to the least which is 1.9 seconds

    Thanks in advance, I really need help with that.
     
    charmseer likes this.
  2. archer_fung_6waves

    archer_fung_6waves

    Joined:
    Jul 2, 2015
    Posts:
    1
    Could you post your current spawning script to have a look?
     
    charmseer likes this.
  3. zioziozio

    zioziozio

    Joined:
    Sep 11, 2012
    Posts:
    10
    We might need to see some code to give good help, but here's some basic ideas:

    Looks like you'll need a couple variables for the spawn timer, and a couple for the big countdown.

    Here's a rough way to do it without any coroutines or timers (does Unity have timers?) It's pseudo code and you'll have to write the real code yourself, but this should give you an idea.

    Code (PSEUDOCODE):
    1. spawnInterval = 3;
    2. currentSpawnTime = 0;
    3.  
    4. bigCountdown = 120; // 120 seconds is 2 minutes
    5. currentBigTime = 0;
    6.  
    7. update(){
    8.   currentSpawnTime += Time.deltatime;
    9.   currentBigTime += Time.deltatime;
    10.  
    11.   if(currentSpawnTime >= spawnInterval){
    12.     spawn();
    13.     currentSpawnTime = 0;
    14.   }
    15.  
    16.   if(currentBigTime >= bigCountDown){
    17.     spawnInterval -= .1;
    18.     currentBigTime = 0;
    19.   }
    20. }
    21.  
    So basically the times are counting up to a certain time. Inside the if statements they check if the values are over the target times. If so, they run what they need to do then reset the timers.

    I hope that helps!
     
    BrokenhearT likes this.
  4. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Oh wow, I'll check it out now, I'll show you my script to spawn the enemies sorry I didn't attach it,
    I'm not really sure it will work for me since I'm having hard time with math :(.

    here's my code for spawning.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemySpawningScript : MonoBehaviour {
    5.    
    6.     public GameObject [] enemies;
    7.     int enemyNo;
    8.     public float maxPos = 2.5f;
    9.     public float delayTimer = 3f;
    10.     float timer;
    11.    
    12.     void Start () {
    13.         timer = delayTimer;
    14.        
    15.     }
    16.    
    17.     void Update () {
    18.        
    19.         timer -= Time.deltaTime;
    20.         if (timer <=0)
    21.         {
    22.             Vector3 enemyPos = new Vector3(Random.Range(-2.8f, 2.8f), transform.position.y, transform.position.z);
    23.             enemyNo = Random.Range (0,27);
    24.             Instantiate (enemies[enemyNo], enemyPos, transform.rotation);
    25.             timer = delayTimer;
    26.         }
    27.     }
    28. }
    so I should add your script in if statement? instead of if (timer <=0) ?

    a bit confuse.
     
  5. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    here's what I did, but it didn't work.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemySpawnerOverTime : MonoBehaviour {
    5.    
    6.     public GameObject [] enemies;
    7.     int enemyNo;
    8.     public float maxPos = 2.5f;
    9.     public float spawnInterval = 3f;
    10.     public float currentSpawnTime = 0;
    11.    
    12.     public float bigCountdown = 120; // 120 seconds is 2 minutes
    13.     public float currentBigTime = 0;
    14.    
    15.     void Start () {
    16.  
    17.     }
    18.    
    19.     void update(){
    20.         currentSpawnTime += Time.deltaTime;
    21.         currentBigTime += Time.deltaTime;
    22.        
    23.         if(currentSpawnTime >= spawnInterval){
    24.             spawn();
    25.             currentSpawnTime = 0;
    26.         }
    27.        
    28.         if(currentBigTime >= bigCountdown){
    29.             spawnInterval -= .1f;
    30.             currentBigTime = 0;
    31.         }
    32.     }
    33.  
    34.     void spawn ()
    35.     {
    36.         Vector3 enemyPos = new Vector3(Random.Range(-2.8f, 2.8f), transform.position.y, transform.position.z);
    37.         enemyNo = Random.Range (0,27);
    38.         Instantiate (enemies[enemyNo], enemyPos, transform.rotation);
    39.     }
    40. }
    41.  
    42.  
    any ideas?
     
  6. zioziozio

    zioziozio

    Joined:
    Sep 11, 2012
    Posts:
    10
    Could you give me more details on what isn't working? Error message, something weird happening, nothing happening.. etc.
     
    BrokenhearT likes this.
  7. zioziozio

    zioziozio

    Joined:
    Sep 11, 2012
    Posts:
    10
    AH! I found it.. I think you need to capitalilze the U in update. update() --> Update()
     
    charmseer and BrokenhearT like this.
  8. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    oh man, that's embarrising lol, sorry I've been up for about 25 hours with this code.

    will check it now ty!!!
     
  9. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Oh man it worked like a charm!! but a quick question, where can I add the value to make the time won't go less than 1.5f?

    and tahnks again!!
     
  10. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Put the if (currentBigTime>=bigCountDown) stuff inside another if statement

    If(spawnInterval >1.5f) {
    if(currentBigTime >= bigCountdown)
    {
    spawnInterval -= .1f;
    currentBigTime = 0;
    }

    }

    So that it will only decrement the interval while it is > 1.5f. Hopefully that works
     
    BrokenhearT likes this.
  11. zioziozio

    zioziozio

    Joined:
    Sep 11, 2012
    Posts:
    10
    Yeah, that or combine them with && (AND) like this:
    Code (CSharp):
    1. if(currentBigTime >= bigCountdown && spawnInterval > 1.5f){
    2.             spawnInterval -= .1f;
    3.             currentBigTime = 0;
    4.         }
     
    BrokenhearT likes this.
  12. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    it worked thank you very very much!! :)
     
  13. arashsh

    arashsh

    Joined:
    Nov 4, 2017
    Posts:
    5
    Hi, how make spawnInterval back to 3? and again after x second back to increase spee?