Is there a standard (efficient) practice for checking if enemies are in attack range of the player?
I am just starting to come up with some AI systems for my enemies, and my first issue is for them to know when the player is close enough for them to engage.
I also want a faction system of sorts, so AI entities can fight with each other depending on circumstances. My first thought was for every entity to check its distance against every other entity with Vector3.Distance and see if it's close enough. Which is an obvious solution but the processor load scales exponentially with more entities. Then again, I am probably only looking at a couple dozen entities in play at any one time so maybe it'd be no big deal to run a few hundred distance checks constantly (and it doesn't need to be every frame).
Plan B was to put trigger colliders on everything that represents their "attention zone" and only interact with things inside of that, but my gut tells me that the colliders are probably more of a system load than the distance checks.
Really I am probably just being paranoid, a modern cpu could probably handle thousands/millions of such checks every frame, but I thought I would ask what standard approach is used for these things, as I am sure this must come up a lot.
Posted Aug 24, 2019, 8:22 PM UTC
Go with plan B, the built in physics engine is much faster then what you are suggesting in plan A.