Can Somone please explain the logic behind this code,what exactly is code doing ?
void FindClosestTarget()
{
Enemy[] enemies = FindObjectsOfType<Enemy>();
Transform closestTarget = null;
float maxDistance = Mathf.Infinity;
foreach(Enemy enemy in enemies)
{
float targetDistance = Vector3.Distance(transform.position, enemy.transform.position);
if (targetDistance < maxDistance)
{
closestTarget = enemy.transform;
maxDistance = targetDistance;
}
}
Target = closestTarget;
}