I have a situation where I want to find the NPC nearest to the PC, but don’t care about anything outside of a given range (e.g. 12 meters)

I guess I might…

  1. Iterate the list of objects tagged ‘npc’ (can be staggered/optimised in various ways)
  2. Use a sphere collider (trigger) to get only what’s in range.

Are sphere colliders (solution 2) suitable / intended for this kind of work, or should I roll my own system (solution 1).
is there a hidden cost to FindGameObjectsWithTag?

Thanks for help.

In my experience all GameObject.Find (…) functions are somewhat expensive. Caching the objects into variables is the safest alternative.
I’m not sure whether SphereCollider objects are using some form of grid-optimization or not, but a (vector1 - vector2).magnitude operation is not extremely expensive as it is. If you’re extremely concerned about optimization you should probably try and benchmark both solutions and see where it takes you :slight_smile:

I’d make a physics layer for “characters”. Then you can put colliders on the characters set to that layer, and Physics.OverlapSphere against that layer (at whatever radius you like) when you want to detect who is nearby.