- Home /
enemy spawner collision check probleme
i am trying to make a spawner that spawn enemy within a radius without spawning them in an existing object. how i do it is i use a sphere that "teleport" within a random position on the x and z axis and check the collision and if there is no collision spawn the enemy. so for the the moment i can spawn them within a radius but im having trouble with detecting if there is no collision. so far i have use OnTriggerEnter/Exit to detect it but when it exit the collision and "teleport" in an other existing object it ignore the collision check and spawn an enemy anyway. it work good when there is not much gameobject in the radius but when there is alot its a real probleme because my enemys get stuck in the walls and map objects. so i whant to know what i did wrong or if there is a better way to do it?
here is all i have done:
var enemy1 : Transform;
var spawnSpace : boolean = true;
var spawnManager : Transform;
var collisionCount = 0;
function Start () {
var spawnSpace = true;
}
function Update () {
if (collisionCount == 0){
spawnSpace = true;
}
else{
spawnSpace = false;
}
}
function OnTriggerEnter (other : Collider) {
collisionCount ++;
}
function OnTriggerExit (other : Collider) {
collisionCount --;
}
function SpawnCheck(){
if (spawnSpace == true ){
Instantiate(enemy1, transform.position, transform.rotation);
spawnManager.GetComponent(Spawner).spawnedEnemy ++;
spawnManager.GetComponent(Spawner).spawnCycle = true;
}
else if (spawnSpace == false){
spawnManager.GetComponent(Spawner).NoSpace();
Debug.Log("no space");
}
}
so is there
have you set a collider on your new objects to being a trigger?
i have double checked my colliders and everyone has a collider and the spawner has the trigger but the thing is when it teleport in a collider it doesnt spawn but if it teleport in an other one right after even if there is a collider it spawn the enemy anyway
Answer by OLP · Oct 02, 2013 at 08:40 PM
Instead of teleporting a Sphere, you should try calling Physics.OverlapSphere at the location where you want to spawn.
Your answer
![](https://koobas.hobune.stream/wayback/20220613115032im_/https://answers.unity.com/themes/thub/images/avi.jpg)