r/Unity3D•
SwillyDo

Is there a standard (efficient) practice for checking if enemies are in attack range of the player?

Question

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

37
darkchowder•

Go with plan B, the built in physics engine is much faster then what you are suggesting in plan A.

3
edh4131•

I would say it depends on how you want their attention zone to be. You can put a collider as you said to determine when something enters the interaction radius.

However, once that collider is entered, you probably want to do some raycasting to determine if the entity that has entered the interaction radius is "visible" and not obstructed by terrain.

I would create a method which you do some calculations when the interaction radius is entered, and then run it periodically until the other entity leaves the interaction radius.

2
Loraash•

Plan B is the standard efficient way. You can also have a large "danger zone" around the player and regular-sized enemy colliders for the same effect.

2
Fellhuhn•

First don't use Distance as it is expensive. Use the squared distance and compare it to the squared threshold (xx+yy <= threshold*threshold).

Then you could use octrees (or quadtrees if it is 2d). Don't know if it is more efficient than any of Unity's solutions though.

1
excentio•

quadtree/octree is usually the basic of any decent physics engine, however you’re totally right about sqrt distance, as I recall there’s even a vector3.sqrtMagnitude call in unity, sqrt is pretty expensive operation in big scales

2
NeedsLoomis•

The nice thing about this over physics is that you don't have to run it every frame. A squared distance check every 0.5 seconds or so is crazy cheap and responsive enough for most game AI.

2
nmkdSCP-3008 Dev•

That's a myth.

V3.Distance is ridiculously fast, no reason to worry about it for less than 1000 calls.

And that squared threshold thing highly depends on hardware and isn't necessarily faster.

0