Search Unity

I want to make an enemy spawn where there are no obstacles within 20 meters of the player's radius

Discussion in 'Scripting' started by YOAJ1, Oct 15, 2017.

  1. YOAJ1

    YOAJ1

    Joined:
    Jul 17, 2014
    Posts:
    26
    I want to make an enemy spawn where there are no obstacles within 20 meters of the player's radius

    Do you have nice idea?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. YOAJ1

    YOAJ1

    Joined:
    Jul 17, 2014
    Posts:
    26
  4. AdamAlexander

    AdamAlexander

    Joined:
    Jul 24, 2015
    Posts:
    73
    Here's a script I've used to spawn gameobjects randomly within a box area which are clear of obstacles. If you tweak it you'll be able to get the behaviour you want. It spawns as many gameobjects as you want up to a maximum. You can set the minimum distance from obstacles and the size of the area to spawn gameobjects in.

    Code (CSharp):
    1. public class BoxSpawner : MonoBehaviour
    2. {
    3.     public GameObject ToSpawn;
    4.     public int Number;
    5.     public float SpawnInterval;
    6.     public int StartSpawnAmount;
    7.  
    8.     public float SizeX = 10f;
    9.     public float SizeY = 10f;
    10.     public float SizeZ = 10f;
    11.  
    12.     public float ClearRadius = 1f;
    13.     public LayerMask ObstructingLayers;
    14.  
    15.     float spawnCountdown;
    16.     GameObject[] spawned;
    17.  
    18.     void Awake()
    19.     {
    20.         spawned = new GameObject[Number];
    21.     }
    22.  
    23.     void Start()
    24.     {
    25.         for (int i = 0; i < StartSpawnAmount; i++)
    26.         {
    27.             spawn();
    28.         }
    29.     }
    30.  
    31.     void OnEnable()
    32.     {
    33.         StartCoroutine(SpawnRoutine());
    34.     }
    35.  
    36.     IEnumerator SpawnRoutine()
    37.     {
    38.         spawnCountdown = SpawnInterval;
    39.         while (true)
    40.         {
    41.             spawnCountdown -= Time.deltaTime;
    42.             if (spawnCountdown <= 0f)
    43.             {
    44.                 spawn();
    45.             }
    46.             yield return null;
    47.         }
    48.     }
    49.  
    50.     int nextAvailableSlot
    51.     {
    52.         get
    53.         {
    54.             for (int i = 0; i < spawned.Length; i++)
    55.             {
    56.                 if (spawned[i] == null) return i;
    57.             }
    58.             return -1;
    59.         }
    60.     }
    61.  
    62.     void spawn()
    63.     {
    64.         spawnCountdown = SpawnInterval;
    65.         var nextSlot = nextAvailableSlot;
    66.         if (nextSlot == -1) return; // No spawn slots available
    67.  
    68.         int nTrys = 0;
    69.         Vector3 pos;
    70.         do
    71.         {
    72.             nTrys++;
    73.             if (nTrys > 10)
    74.             {
    75.                 Debug.LogWarning("Failed to find spawn location after 10 tries, aborting.", gameObject);
    76.                 return;
    77.             }
    78.             pos = chooseLocation();
    79.         } while (locationIsObstructed(pos));
    80.  
    81.         var newInst = Instantiate(ToSpawn, pos, transform.rotation) as GameObject;
    82.         newInst.transform.SetParent(transform.parent);
    83.         spawned[nextSlot] = newInst;
    84.     }
    85.  
    86.     Vector3 chooseLocation()
    87.     {
    88.         var dims = new Vector3(SizeX / 2f, SizeY / 2f, SizeZ / 2f);
    89.         var randVector = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), Random.Range(-1f, 1f));
    90.         var pos = Vector3.Scale(dims, randVector) + transform.position;
    91.         return pos;
    92.     }
    93.  
    94.     bool locationIsObstructed(Vector3 location)
    95.     {
    96.         return Physics.CheckSphere(location, ClearRadius, ObstructingLayers);
    97.     }
    98.  
    99.     protected static readonly Color NoneColor = Color.green;
    100.     protected static readonly Color RedColor = Color.red;
    101.     public void OnDrawGizmosSelected()
    102.     {
    103.         if (!isActiveAndEnabled) return;
    104.  
    105.         else Gizmos.color = NoneColor;
    106.         Gizmos.DrawCube(transform.position, new Vector3(SizeX, SizeY, SizeZ));
    107.  
    108.         Gizmos.color = RedColor;
    109.         Gizmos.DrawSphere(transform.position + Vector3.up * (SizeY / 2f + ClearRadius), ClearRadius);
    110.     }
    111. }
     
  5. YOAJ1

    YOAJ1

    Joined:
    Jul 17, 2014
    Posts:
    26

    thanks to you problem solved!!!!