- Home /
 
               Question by 
               Legendpc · Apr 11, 2020 at 10:58 AM · 
                spawningenemiesenemy spawn  
              
 
              I want that the enemies spawn in a random radius from the player, but not on other gameobjects like a housen or a car or something.
I have programmed a enemies spawner that spawns enemies in a random circle around the player. But the enemies keep spawning on other gameobjects like houses. Is there anyway to evoide that? 
And here is my script: using System.Collections; using System.Collections.Generic; using UnityEngine;
 public class EnemySpawner : MonoBehaviour
 {
     [SerializeField]
     public float spawnRadius = 7, time = 2.50f;
     public int Enemysnow = 0;
     public int enemyMax = 35;
     private bool maxreached = false; // Bool that stores the Information if the maximum is reached
 
     public GameObject[] enemies; //List of enemies that can get spawned
 
     // Start is called before the first frame update
     void Start()
     {
         StartCoroutine(SpawnEnemy()); //Start the Couroutine once at the start of the game
     }
 
     IEnumerator SpawnEnemy() // that is the important part
     {
         while (true)
         {
             if (!maxreached)
             {
                 Vector2 spawnPos = GameObject.Find("Player").transform.position; //Spawning an enemy
                 spawnPos += Random.insideUnitCircle.normalized * spawnRadius;
 
                 Instantiate(enemies[Random.Range(0, enemies.Length)], spawnPos, Quaternion.identity);
                 yield return new WaitForSeconds(time);
                 Enemysnow++; // increase the enemy counter
             }
             yield return null;
         }
     }
 
     public void lowerMaxEnemys()
     {
         Enemysnow--; // decrease the enemy counter if a enemy is killed(Kill is in another script)
     }
     public void Update()
     {
             maxreached = Enemysnow >= enemyMax;
     }
     public void HigherMaxEnemys(int amount)
     {
         enemyMax += amount;
     }
     public void HigherSpawnRade(float amount)
     {
         time -= amount;
     }
 }
 
 
                 
                unity-forum-help.png 
                (58.9 kB) 
               
 
              
               Comment
              
 
               
              Answer by endasil_unity · Apr 20, 2020 at 06:25 AM
I would solve it by checking if there is an object at the spawn location or not. Something like this: if (!Physics.CheckSphere (spawnPos, size)) { Instantiate(enemies[Random.Range(0, enemies.Length)], spawnPos, Quaternion.identity); }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                