Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

can I lock down my random spawner or improve upon it?

Discussion in 'Scripting' started by San_Holo, Nov 25, 2014.

  1. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    Hi,

    You'll see from the code below I've got a pool of prefabs which I've set to be randomly picked which are then positioned randomly above my view-port or game play area and it does the job, but it's a bit hap hazard, too much... what I'm trying to do it make it a little bit more uniform, for example my minimum Y position could be calculated far too high as you wait for the first wave to float in upon starting... I'm really wanting to randomly spawn my objects along a set X axis which I'm doing, then moving on up the Y axis by a set amount (not too close to each other) and then spawning, so it should iterate through and up the screen these little waves of aliens and yes I'm having trouble making it a little more interesting... the game scene itself will repeat and variables will change accordingly to alter the wave amounts and frequency etc...

    Anyway the code below works, needs a better eye perhaps, please advise, thanks

    Code (csharp):
    1. public class LevelGen : MonoBehaviour
    2. {      
    3.         public GameObject[] wavePool;
    4.         public float minX;
    5.         public float maxX;
    6.         public float minY;
    7.         public int waveAmount;
    8.         private float waveSpace = 2.0f;
    9.         private GameObject wave;
    10.         public GameObject Boss;
    11.         Vector2 newPosition;
    12.    
    13.         void Start ()
    14.         {
    15.                 createLevel ();
    16.         }
    17.    
    18.         void createLevel ()
    19.         {
    20.                 for (int i = 0; waveAmount >= 1; i++) {
    21.                         wavePosition ();
    22.                         wave = wavePool [Random.Range (0, wavePool.Length)];
    23.                         Instantiate (wave, newPosition, transform.rotation);
    24.                         waveSpace = waveSpace + 5.0f;
    25.                         waveAmount--;
    26.                         if (waveAmount == 0) {
    27.                                 Boss.SetActive (false);
    28.                                 Instantiate (Boss, newPosition, transform.rotation);
    29.                         }
    30.                 }
    31.         }
    32.  
    33.         void wavePosition ()
    34.         {
    35.                 float posX = Random.Range (minX, maxX);
    36.                 float posY = minY + waveSpace;
    37.                 newPosition = new Vector2 (posX, posY);
    38.         }
    39. }
    40.  
     
    Last edited: Nov 25, 2014
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    I would recommend changing your minX/maxX, etc. values to be float instead of int.

    That way when you hand them to Random.Range(), you will be using the float version of Random.Range(), and hence it will be possible for the positions to take on non-integer values.

    The way it works is, if you say Random.Range( 0, 5) (by using ints), you will only get 0, 1, 2, 3 or 4 as possible values back.

    If you use the float version, you will get numbers of all possible values between 0.0f and 5.0f.

    Furthermore, if you're raining them in from offscreen, make the vertical min/max relatively close together, and that will eliminate the initial lag.

    Kurt
     
    San_Holo likes this.
  3. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    Thanks Kurt