Can someone explain how Mathf.infinity is working in this script? The script is supposed to turn an instantiated bullet towards the closest target, but I can't really tell what Math.infinity is actually doing.
// Update is called once per frame
void Update ()
{
float nearestDistance = Mathf.Infinity;
GameObject nearestEnemy = null;
foreach(GameObject obj in GameObject.FindGameObjectsWithTag("Enemy"))
{
//float distance = Vector3.Distance(transform.position - obj.transform.position);
float distance = (transform.position - obj.transform.position).sqrMagnitude;
if(distance < nearestDistance)
{
nearestDistance = distance;
nearestEnemy = obj; // set 1 gameObject
}
}
if(nearestEnemy != null)
{
transform.rotation = Quaternion.LookRotation(nearestEnemy.transform.position - transform.position);
}
}
↧