Why do we use Mathf.Infinity to find closest target ?

How many enemies you got? You can try using my distance comparator for point lists, here . But in order for them to work as intended, you either check a few points which you can enter directly as argument, or you’re supposed to prepare a list (or array) of them in advance.

I can also help you customize this approach so that it searches through an array of transforms which you can maybe get with FindGameObjectsWithTag or a similar technique.

If your enemies are numerous but usually likely to be outside some maximum range – i.e. you are consistently wasting time by observing/measuring distance to enemies that are too far away anyway – you should probably use a dedicated spatial algorithm to optimize the solution. Though you’d probably have a hard time doing this considering you’re still learning.

Regarding your original question, I believe MelvMay told you everything there is to know in the 1st reply.