Graphics Reference
In-Depth Information
// now we go through any transforms 'under' this transform,
// so all of the child objects that act as our waypoints get
// put into our arraylist
foreach(Transform t in transform)
{
// add this transform to our arraylist
transforms.Add(t);
}
totalTransforms=(int)transforms.Count;
}
public int FindNearestWaypoint ( Vector3 fromPos, 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 closer than the closest object
// (whose distance is held by the variable
// (distance)
if ( curDistance < distance )
{
if( Mathf.Abs( TEMPtrans.position.y -
fromPos.y ) < maxRange )
{
// 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