Graphics Reference
In-Depth Information
With the addition of the maxRange check, we can no longer be 100% sure of a return
result, so a quick check makes sure that closest has something other than a zero in it before
returning the result:
// now we make sure that we did actually find
// something, then return it
if(closest)
{
// return the waypoint we found in this test
return TEMPindex;
} else {
// no waypoint was found, so return -1 (this
// should be accounted for at the other end!)
return -1;
}
}
// this function has the addition of a check to avoid finding the
// same transform as the one passed in. We use this to make sure
// that when we are looking for the nearest waypoint we don't find
// the same one as we just passed
It is possible to have two different implementations of the same function, each with
its own set of required parameters. Here, a second version of the FindNearestWaypoint()
function follows almost the same course as the first with the exception of a new transform
passed in via the exceptThis parameter discussed earlier in this section.
public int FindNearestWaypoint (Vector3 fromPos , Transform
exceptThis, float maxRange)
{
// make sure that we have populated the transforms
// list, if not, populate it
if(transforms==null)
GetTransforms();
// 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;
// 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 && TEMPtrans !=
exceptThis )
Search WWH ::




Custom Search