How do you find the furthest GameObject in an array. I know how to find the closets:
GameObject FindLastWaypoint()
{
WayPoints = GameObject.FindGameObjectsWithTag("Waypoint");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject item in WayPoints)
{
Vector3 diff = item.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = item;
distance = curDistance;
}
}
return closest;
}
But I can’t find the furthest! HELP!!