Find Objects Which is Out Of OverlapSphere
Hi guys,
I can find objects which collides with OverlapSphere below,
---Collider[] hitColliders = Physics.OverlapSphere(player.transform.position, zoneRadius, LayerMask.GetMask("SP"));
But, I need to find gameobjects which is out of OverlapSphere.
Do you have any idea ?
Answer by goutham12 · Sep 27, 2019 at 06:33 AM
I don't know if there is direct method for that. but you try like this
List<GameObject> GetObjectsOutSideOftheSpehere(){
List<GameObject> finalList = new List<GameObject>();
GameObject[] allObjects = GameObject.FindGameObjectsWithTag("your tag");
for(int i=0; i < allObjects.Length; i++){
if(Vector3.Distance(allObjects[i].transform.Position,transform.Position) >
SphereRadius){
finalList.add(allObjects[i]);
}
}
return finalList;
}
if this is not what you want try to explaing what you want to do with that objects so that some one can tell you properly.
Hey @goutham12 I've a player and spawn points around it. I spawn enemies at spawnpoint randomly. But it may spawn enemy where player is at. I don't want this coincidence to come true. So i created a OverlapSphere around player. ~~~I'm trying to do is: Spawn(instantiate) enemies at spawnpoints which are away of player. But I'm stuck and can't move on. If anyone can help me, i be happy.