Search Unity

Spawning iterms within a radius?

Discussion in 'Scripting' started by Rastafari, Jan 7, 2013.

  1. Rastafari

    Rastafari

    Joined:
    Jul 18, 2011
    Posts:
    196
    Hello ladies and gentlemen.
    I was wondering how I create objects spawn within a radius.

    As an example
    I have a cube 5x1x1
    I want to have objects spawn inside the objects size.
    I hope you understand what I mean.
     
  2. Deleted User

    Deleted User

    Guest

    If it doesnt need to be a cube you can use
    Code (csharp):
    1. Random.insideUnitSphere
    or
    Code (csharp):
    1. Random.insideUnitCircle
    for a cube you need to do somethink like
    Code (csharp):
    1.  
    2. //Pseudocode
    3. x = Random.Range(0f, cube.extends.x);
    4. // same for y and z
    5.  
    6. transform.position = new Vector3(x, y, z);
    7.  
    I have a spawning script somewhere around here. If you want I can post it
     
  3. Rastafari

    Rastafari

    Joined:
    Jul 18, 2011
    Posts:
    196
    If you would be that kind , thank you.
     
  4. Deleted User

    Deleted User

    Guest

    It is quit old and there are maybe some nooby mistakes in it :) .

    SpawnArea.cs
    Code (csharp):
    1.  
    2. // place this on an empty GameObject
    3. // the radius is defined by the GameObjects scale
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. public class SpawnArea: MonoBehaviour {
    8.     // Array of Mobs
    9.     public GameObject[] mobs;
    10.  
    11.     public int totalMobsPosible = 2;
    12.     public int mobsInArea;
    13.     public LayerMask groundLayer;
    14.    
    15.    
    16.     // Spawn Are Radius (set by Scale)
    17.     private float SpawnRadius = 10f;
    18.     // Color for Girmo Cyrcle
    19.     private Color _gizmoColor = Color.red;
    20.    
    21.     // cached transform
    22.     private Transform tr;
    23.    
    24.     void Awake(){
    25.         tr = gameObject.transform;
    26.     }
    27.    
    28.     void Update(){
    29.         SetRadius();
    30.     }
    31.    
    32.    
    33.     public void Spawn(){
    34.         mobsInArea++;
    35.         GameObject clone = GetRandomMob();
    36.         clone = Instantiate(mobs[0], GetPosition(), Quaternion.identity) as GameObject;
    37.         clone.GetComponent<AI>().mySpawnArea = GetComponent<SpawnArea>();
    38.        
    39.         // set the Y coordinate to y + the half height of the CharacterController
    40.         float height = (float)(clone.GetComponent<CharacterController>().height * 0.5);
    41.         clone.transform.position = new Vector3(clone.transform.position.x, clone.transform.position.y + height, clone.transform.position.z);
    42.        
    43.     }
    44.    
    45.     // get a random position inside the Spawn Area
    46.     public Vector3 GetPosition(){
    47.         Vector2 posInCyrcle = Random.insideUnitCircle * SpawnRadius;
    48.         Vector3 position = new Vector3(posInCyrcle.x + tr.position.x, 10000, posInCyrcle.y + tr.position.z);
    49.         RaycastHit hit;
    50.        
    51.         if(Physics.Raycast(position, -Vector3.up, out hit, Mathf.Infinity, groundLayer)){
    52.             position.y = hit.point.y;
    53.         }else
    54.             position.y = transform.position.y;
    55.         return position;
    56.  
    57.     }
    58.    
    59.     // get a random Mob and returns it
    60.     private GameObject GetRandomMob(){
    61.         return mobs[Random.Range(0, mobs.Length)];
    62.     }
    63.    
    64.     private void SetRadius(){
    65.         SpawnRadius = (tr.localScale.x * tr.localScale.y * tr.localScale.z) / 3;
    66.     }
    67.    
    68.     void OnDrawGizmos () {
    69.         tr = gameObject.transform;
    70.         SetRadius();
    71.        
    72.         Vector3 currentWaypoint = transform.position;
    73.        
    74.         Debug.DrawLine (transform.position, currentWaypoint, _gizmoColor);
    75.        
    76.         Vector3 pP = currentWaypoint + SpawnRadius * new Vector3 (1,0,0);
    77.         for (float i = 0; i < 2 * System.Math.PI; i+= 0.1F) {
    78.             Vector3 cP = currentWaypoint + new Vector3 ((float)System.Math.Cos (i)*SpawnRadius,0,(float)System.Math.Sin(i)*SpawnRadius);
    79.             Debug.DrawLine (pP,cP, _gizmoColor);
    80.             pP = cP;
    81.         }
    82.         Debug.DrawLine (pP, currentWaypoint + SpawnRadius*new Vector3 (1,0,0), _gizmoColor);
    83.     }
    84.    
    85. }
    86.  
    87.  
    SpawnManager .cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SpawnManager : MonoBehaviour {
    6.    
    7.     public float refreshRate = 2.0f;
    8.     public int maxIteration = 3;
    9.     public SpawnArea[] _spawnAreas;
    10.    
    11.     private float _counter;
    12.    
    13.     void Start () {
    14.         _counter = refreshRate;
    15.         _spawnAreas = FindObjectsOfType(typeof(SpawnArea)) as SpawnArea[];
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.         _counter += Time.deltaTime;
    21.        
    22.         if(_counter >= refreshRate){
    23.             _counter -= refreshRate;
    24.             for(int i = 0; i < _spawnAreas.Length; ++i){
    25.                 for(int x = 0; x <= maxIteration; ++x){
    26.                     if(_spawnAreas[i].mobsInArea < _spawnAreas[i].totalMobsPosible){
    27.                     _spawnAreas[i].Spawn();
    28.                     }
    29.                 }
    30.             }          
    31.         }
    32.     }
    33.    
    34.    
    35.    
    36. }