Graphics Reference
In-Depth Information
// the distance variable is just used to hold the
// 'current' distance when we are comparing, so that
// we can find the closest
distance = Mathf.Infinity;
To find the nearest waypoint to the 3D vector fromPos, the variable distance starts
out at a very large number—Mathf.Infinity. We use this to compare distances during the
iteration loop below:
// Iterate through them and find the closest one
for(int i = 0; i < transforms.Count; i++)
{
// grab a reference to a transform
TEMPtrans = (Transform)transforms[i];
// calculate the distance between the current
// transform and the passed in transform's
// position vector
diff = (TEMPtrans.position - fromPos);
curDistance = diff.sqrMagnitude;
// now compare distances - making sure that
// we are not
if ( curDistance < distance )
{
As Mathf.Infinity is, well, infinity, this condition will always be satisfied at least once.
There should always be something closer than infinity, meaning that as long as there are
transforms to compare, we should always get at least some kind of return result no matter
how far away they are in the 3D world.
if( Mathf.Abs( TEMPtrans.position.y -
fromPos.y ) < maxRange )
{
In some case, returning far away transforms may not always be the desired result, so
maxRange is provided to at least have some kind of cap on distance if needed.
// set our current 'winner'
// (closest transform) to the
// transform we just found
closest = TEMPtrans;
// store the index of this
// waypoint
TEMPindex=i;
// set our 'winning' distance
// to the distance we just
// found
distance = curDistance;
}
}
}
Search WWH ::




Custom Search