Search Unity

Random position spawning

Discussion in 'Scripting' started by DarkNeo, Jul 23, 2018.

  1. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Hi there, I am using the following code to generate some prefabs to spawn and that works great however I would love it for the spawning to be random across the entire screen. I only need it to be random in the X and Y direction as it's a 2d game.

    Below is my current spawning code. Is there a way to use Random Range to randomly spawn the positioning of the object. Thank you :)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SpawnScript : MonoBehaviour {
    5.  
    6.     public GameObject[] obj;
    7.     public float spawnMin = 1f;
    8.     public float spawnMax = 2f;
    9.  
    10.    
    11.  
    12.      void Start ()
    13.    
    14.     {
    15.         Spawn();
    16.  
    17.     }
    18.  
    19.  
    20.     void Spawn ()
    21.     {
    22.  
    23.         Instantiate(obj[Random.Range (0, obj.GetLength(0))], transform.position, Quaternion.Euler(0, 0, -360));
    24.  
    25.         Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
    26.  
    27.     }
    28.  
    29.  
    30. }
    31.  
    32.  
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Random.range gives you one random number between a low number and a high number. You just need to call it twice, once for x coordinate and once for y coordinate. Then spawn at those coordinates. E.g. random.range(0,Screen.width-1) And Random.range(0,Screen.height-1)
     
    DarkNeo likes this.
  3. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    depends on your coordinate system and scale though as to how much coordinates cover the screen. you can do random.value()*width whatever the units is.
     
    DarkNeo likes this.
  4. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Cheers I will have to have a play with it, I'm really new at coding :) thank you for the reply
     
  5. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Basically got it working with this. Values need tweaking but its giving a decent result so far.


    Code (CSharp):
    1. public GameObject[] obj;
    2.     public float spawnMin = 1f;
    3.     public float spawnMax = 2f;
    4.     public float horizontalMin = 1f;
    5.     public float horizontalMax = 5f;
    6.     public float verticalMin = -2f;
    7.     public float verticalMax = 2f;
    8.  
    9.     private Vector2 originPosition;
    10.     private Vector2 originDimentions;
    11.    
    12.      void Start ()
    13.    
    14.     {
    15.  
    16.         originDimentions = transform.localScale;
    17.         originPosition = transform.position;
    18.         Spawn();
    19.  
    20.     }
    21.  
    22.     void Spawn ()
    23.     {
    24.  
    25.  
    26.         Vector2 randomPosition = originPosition + new Vector2(Random.Range(horizontalMin, horizontalMax), Random.Range(verticalMin, verticalMax));
    27.         Instantiate(obj[Random.Range (0, obj.GetLength(0))], randomPosition, Quaternion.Euler(0, 0, -360));
    28.         originPosition = randomPosition;
    29.      
    30.         Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
    31.          
    32.     }