Math in Unity - Calculate the way to the closest enemy

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

Timo Schmid
Jun 17, 2021 · 3 min read
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:

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!

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Timo Schmid

Written by

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 :)

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.