How to detect closest enemy

From Beta Forum

Q: How can I sort all of the objects in a zone? Basically I’m looking to make a GameObject[] enemyArray of enemies in an OverlapSphere so I can then sort by distance. Anyone have any advice?

  QA Team

If you simply want to know which is the closest enemy to the player (or something else) you can do this.
You need a trigger zone set to detect Tag’s called distance which has zone size 0 and falloff size big enough to encompass your enemies to check the distance. The closest Tag called distance within the falloff will have the highest value.
On the enemies you have a Tag called Distance wired to a Gate gadget set to interrupt. Only one Gate gadget of the same name can be active at a time. As the tags move closer to the centre of the zone their value increases. The closest tag will have the highest value and its gate will become the active Gate
You can then have a microchip activated by the Gate with A Tag saying closest target or whatever logic you want the closest target to have.
Hope that helps get you started.

Comments and Feedback

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a free website or blog at

Up ↑

%d bloggers like this: