Math in Unity - Calculate the way to the closest enemy

Hmm… Which enemy will be the right one to destroy?

Timo Schmid
Nerd For Tech
3 min readJun 17, 2021

--

Photo by GR Stocks on Unsplash

There might be some time when you develop a game where you need to calculate the closest enemy out of all on the screen. Let that be because you want to implement a powerup which targets the nearest enemy or have another idea why you want to do that. Let’s figure out how to do that in todays article!

I. The objective
We have a player and two enemies on the screen. As we only want to target the closest enemy, we need to calculate the closest one. The chosen enemy shall be printed out into the console.

II. Theory - what do we have to do?
To be able to calculate the closest enemy, we first have to detect if there are any GameObjects. Compare that with an infrared laser. The laser has a straight line and as soon as something is in between it, it will break, or shorten. Then, the laser does know that there is something in between. If installed into an alarm system, that would be the trigger for the alarm system to go on. We will do the same with Mathf.Infinity which basically is like a infrared sensor here. It will draw a line throughout the whole world space and will be cut when there is something in between.

After we know that there is at least one GameObject in the scene, we need to calculate the distance for each GameObject in the scene.

Next thing is to compare all calculated distances and choose the closest or smallest one.

Personally, I had no idea of how to compare all the directions and googled it. With that research, I also found some optimization tips as well!
Here, take a look:

For anyone who arrives here through a Google search, the presented answer by tomvcds is good, but should definitely be optimized by comparing distance squared instead of straight distance. It adds no complexity to the code and is more efficient, since it avoids the expensive square root operation that happens under the hood by doing “Vector3.Distance”.

For example — Say you have 3 objects, A, B, and C, and a source. You want to know which one is closest to the source. Let’s say A is 2m away, B is 3m away, and C is 4m away. Instead of doing the distance test and getting 2, 3, and 4, you can instead get the value in distance squared, which would be 4, 9, and 16, respectively. Distance squared still maintains their order by proximity, it’s just that the values increase exponentially instead of linearly.

III. The implementation - Coding what we need
First of all, we need an array which stores all enemies. As we are interested in the distances, we need to create it with type Transform:

To create the infrared laser like detection system in Unity, we can use Mathf.Infinity.

Now we need a loop. We want to calculate the direction for every enemy, the best loop we can use is a foreach loop:

  • First, we calculate the direction
  • Second, we calculate the square magnitude
  • Third, we assign the closest enemy as potential target

IV. The finished end result
Running this code in Unity, will give us the following end result:

Thanks for your time and interest!
Catch you next time!

--

--

Timo Schmid
Nerd For Tech

The mission? Becoming a game developer! RPG is the dream! Writing down my journey here for me and for everyone interested. Thanks for showing interest :)