Search Unity

Question about this script,

Discussion in 'Scripting' started by BrokenhearT, Jun 30, 2015.

  1. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Hello everyone
    I have a script to spawn the enemy, how can I get it to increase the spawn over time? and also not exceeding certain amount? if there's any tutorial or anyone can help would be great

    here's the code.

    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 = 0.5f;
    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,28);
    24.             Instantiate (enemies[enemyNo], enemyPos, transform.rotation);
    25.             timer = delayTimer;
    26.         }
    27.     }
    28. }
     
  2. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    I've never done this but my immediate thought would be to do something like (note, out-of-head and untested):

    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.     private float lastSpawnTime;
    10.     private float spawnDelay = 5f; // starting spawn delay, once every 5 second
    11.     private float minSpawnDelay = 0.5f; // min delay between spawns, currently 2 times per second
    12.     private float spawnDelayAdjustmentFactor = 0.9f; // value to multiply the spawnDelay each time.
    13.  
    14.     //
    15.     // Note the spawnDelayAdjustmentFactor could be a value that you subtract from the spawnDelay
    16.     // instead of multiplying - it just depends on how you want it to behave.
    17.     //
    18.  
    19.     void Start () {
    20.         lastSpawnTime = 0;//Time.time;      
    21.     }
    22.  
    23.     void Update () {
    24.      
    25.         float timeSinceLastSpawn = Time.time - lastSpawnTime;
    26.         if (timeSinceLastSpawn >= spawnDelay)
    27.         {
    28.             lastSpawnTime = Time.time;
    29.  
    30.             if(spawnDelay > minSpawnDelay)
    31.             {
    32.                 spawnDelay = spawnDelay * spawnDelayAdjustmentFactor;
    33.                 if(spawnDelay < minSpawnDelay)
    34.                 {
    35.                      spawnDelay = minSpawnDelay;
    36.                 }
    37.             }
    38.  
    39.             Vector3 enemyPos = new Vector3(Random.Range(-2.8f, 2.8f), transform.position.y, transform.position.z);
    40.             enemyNo = Random.Range (0,28);
    41.             Instantiate (enemies[enemyNo], enemyPos, transform.rotation);            
    42.         }
    43.     }
    44. }
     
    BrokenhearT likes this.
  3. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Gonna try it and be with you thanks a bunch in advance!!
     
  4. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Thank you very much for you help really appreciated but I'm really bad with math, which is why I'm not sure if what I did was right, I wanted to reduce the spawn time every a min and a half by 1 second until it goes down to a second and half per spawn won't go below that so I did this to your adjustment on my script not sure if that's right though can you have a look ?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SpawningTimerForIncreasingSpawnRate : MonoBehaviour {
    5.  
    6.        
    7.     public GameObject [] enemies;
    8.     int enemyNo;
    9.     public float maxPos = 2.5f;
    10.     private float lastSpawnTime;
    11.     private float spawnDelay = 3f; // starting spawn delay, once every 5 second
    12.     private float minSpawnDelay = 90f; // min delay between spawns, currently 2 times per second
    13.     private float spawnDelayAdjustmentFactor = 0.1f; // value to multiply the spawnDelay each time.
    14.        
    15.         //
    16.         // Note the spawnDelayAdjustmentFactor could be a value that you subtract from the spawnDelay
    17.         // instead of multiplying - it just depends on how you want it to behave.
    18.         //
    19.        
    20.     void Start ()
    21.     {
    22.         lastSpawnTime = 0;//Time.time;    
    23.     }
    24.        
    25.  
    26.     void Update ()
    27.     {
    28.            
    29.         float timeSinceLastSpawn = Time.time - lastSpawnTime;
    30.         if (timeSinceLastSpawn >= spawnDelay)
    31.             {
    32.             lastSpawnTime = Time.time;
    33.                
    34.             if(spawnDelay > minSpawnDelay)
    35.             {
    36.                 spawnDelay = spawnDelay - spawnDelayAdjustmentFactor;
    37.                 if(spawnDelay < minSpawnDelay)
    38.                 {
    39.                     spawnDelay = minSpawnDelay;
    40.                 }
    41.             }
    42.  
    43.                 Vector3 enemyPos = new Vector3(Random.Range(-2.8f, 2.8f), transform.position.y, transform.position.z);
    44.                 enemyNo = Random.Range (0,27);
    45.                 Instantiate (enemies[enemyNo], enemyPos, transform.rotation);          
    46.             }
    47.         }
    48.     }

    Thanks again.