Mathf.Infinity in Unity
Objective: Understand why we use Mathf.Infinity to calculate the closest target.
Note: To understand this better, check my article on how to calculate the closest Enemy. here is a link https://suleimanabdullah.medium.com/learn-to-code-by-making-a-2d-space-shooter-game-23874618595d
But Before going into that let's say we have to define the variable required.

When the script is enabled we set the closest enemy value to null and we assign closestEnemyDistance to Mathf.Infinity.

you may ask why Mathf.Infinity why not normal value?.
Let's say we have three enemies the first one is 7000 meters away from a player, the second is 1000 meters away from the player and the third enemy is 50000 away from the player.

Now let's find the closestEnemy, it doesn't matter which way you use to find the enemy.

When searching for enemies remembers in unity it searches by order so it starts with a first game object with that tag in the hierarchy. Maybe the first object to be searched is the enemy which is 50000 meters away from the player because in the order it appears first.


We iterate inside for each loop we take the first game object we found and go inside foreach before searching for the second enemy.

. Then we go to the if condition, let's say the current distance is 50000 of the first enemy found on the loop, now we check if the current distance is less than the closest Enemy Distance. For the first time when the script is enabled the closest distance is equal to Mathf.Infinity that is equal to a positive infinity value.


Note: If we Initialize the closest Enemy Distance as 10000 we won't be able to iterate to another enemy to calculate their distance.
But since we initialize closest Enemy Distance with the value of Mathf.Infinity, we are able to go inside the if condition since for the first time the current distance is always less than ClosetEnemy Distance since the value of it is Mathf.Infinity.


So when the condition becomes true, we assign the currentDistance of the first enemy found to the closest Enemy Distance. This means we take the closest Enemy Distance and set it with a new value which is the distance of the first enemy we find from the foreach loop, which is 50000 meters away from the player then we set the enemy game object which we found when iterating in foreach as the closest Enemy.

In the second Iteration, let's say the second enemy is 7000 away from the player when we compare them inside the if condition.Here we set closestEnemyDistance as 7000 and set this enemy as closestEnemy.



As for the third enemy which is 1000 away from the player. We check if the ClosestEnemyDistance is less than currentDistance.since 1000 is less than 7000 meters, we set the closestEnemyDistance to 1000 and set this enemy as the ClosestEnemy.


The loop will repeat the iteration again and will check if 50000 as the current Distance is less than 1000 and will check if 7000 as the current Distance is less than 1000 . Here 1000 will be updated again as closestEnemyDistance as long the player is close to this enemy.
According to the number of enemies we have the Closest Enemy is which is 1000 meters away from the player. It can be anything that calculates the closest target, it can be a missile, finding the shortest path if is a racing game, etc.
Note: If we didn't use Mathf.Infinity and say put other value we won't able to succeed finding the closest object cos the first object may have the higher value than guessing the value of the ClosestEnemyDistance.
So it’s I deal to set the ClosestDistanceEnemy/ClosestDistanceTarget as an Infinity number when the object is enabled/spawned/collected also since when checking the condition for the first-time the first object distance will be less than the Infinity Value this Allows us to check all target and make comparison with each Possible to get the Closest Target.
Bye, see you in the next article.
