- Home /
overlapsphere to destroy NPCs on exit
Hi, I'm new to overlapshpere and have been using it to activate spawners in the player's vicinity but I thought I could also use it to destroy NPCs that wander way out of range. I have a population manager script that creates lists of NPCs and has the overlap sphere on it; this is attached to my player.
Some issues I'm running into are:
how to use overlapsphere to detect if an NPC has left exited the trigger zone, since the list of NPCs is changing all the time and overlapsphere seems dependent on arrays
how to differentiate between different sphere colliders on the same object. I have two sphere colliders testing for different ranges, I have assigned them to different variables, but I'm having difficulty differentiating between them in the code. I would like to disable mesh renderers if the NPC leaves the closest sphere and destroy if it leaves the larger one to help with performance.
Is it better to simply use a sphere collider exit instead of overlapsphere to test for exiting NPCs from the list of NPCs?
I appreciate any guidance, general or specific, to help me understand overlapsphere and how to properly implement it.
Here's the code I have so far, which creates lists and does the overlapsphere activation/deactivation of the spawners:
public class populationmanager : MonoBehaviour
{
//layermasks
public LayerMask spawner, allnpc;
public List<GameObject> allnpcs;
public SphereCollider enablesphere, disablesphere;
Collider[] spawners, npcs;
private void OnTriggerEnter(Collider other)
{
if (other.GetComponent<spawner>() != null)
{
ObjectComponentState(other, true);
}
}
private void OnTriggerExit(Collider other)
{
ObjectComponentState(other, false);
if (other.GetComponent<spawner>() != null)
{
other.GetComponent<spawner>().StopAllCoroutines();
}
}
private void ObjectComponentState(Collider other, bool state)
{
if (other.GetComponent<spawner>() != null)
{
other.GetComponent<spawner>().enabled = state;
}
}
private void FixedUpdate()
{
spawners = Physics.OverlapSphere(transform.position, enablesphere.radius, spawner);
for (int i = spawners.Length - 1; i > -1; i--)
{
ObjectComponentState(spawners[i], true);
}
}
}
Answer by unity_ek98vnTRplGj8Q · Feb 25, 2020 at 05:25 PM
If you want to use overlap sphere, you would have to maintain a list of active objects and then check every frame to see if an object in that list did not show up in the next sphere cast. This is definitely not an efficient way to do this.
Yes using OnTriggerExit would be much better as calculating collisions is much less expensive than a spherecast.
If you only have 1 spawn area, then it would be most efficient just to get rid of the large colliders all together and check the distance from each object to the center point of the spawn area
Thank you! Sometimes it just takes writing out the problem and hearing back from more experienced people such as yourself to have things "click."
Since I am making an open world game with many different spawners, I will likely use the sphere overlap for that but I will keep your alternative in $$anonymous$$d. Thank you for clearing up with ontriggerexit piece!
I'm curious if you could expand on number 3 a bit because, though my spawn area is around my moving player, it really does amount to only one area of spawning at a time. Are you saying that it is better for performance to check each spawner's distance to the moving player each frame than to check with an overlapsphere?
Yes, here's why:
OverlapSphere is a physics query, it goes into Unity's physics system and uses some complex algorithms to find exactly what objects are in or touching the given sphere. This could potentially check against every object in your scene including invisible objects, terrain, scene details, etc. which must each take into account their OWN colliders to calculate overlap.
For a distance check all you are doing is doing some simple Vector addition. Yes you are doing it for each NPC, but the overhead here is so greatly reduced that it's ok. THAT BEING SAID, depending on how your game works it is possible that OverlapSphere is better. If you have a significant number (several hundred, maybe several thousand) of NPC's then maybe it is worth revisiting exactly how you are doing this and investigating more effective optimizations
It doesn't matter whether or not the player is moving for a distance check because you don't have to do any extra work to find the position values, they are already calculated for you each frame. However just having colliders that move around in the world (even if you don't use them) has an associated overhead because these colliders must be maintained in Unity's physics system (moving a collider requires a lot of extra calculation behind the scene).
Your answer
Follow this Question
Related Questions
How to design an Archer Board Scoring System 1 Answer
Trigger to some, Collider to others 1 Answer
Compound collider object behaving as a whole? 1 Answer
Detect collider collision ,identify and access colliding colliders 0 Answers
How having correct bounce on a rotating pinwheel(it's more complicated)? 0 Answers