Graphics Reference
In-Depth Information
make sure that more than one waypoint was found. If totalWaypoints is zero, another
attempt is made to get the total from the myWayControl object before it drops out of the
UpdateWaypoints() function:
if( totalWaypoints==0 )
{
// grab total waypoints
totalWaypoints= myWayControl.GetTotal();
return;
}
If no currentWaypointTransform object has been set up at this point, myWay-
Control is called upon to provide one with the index number held in the variable
currentWaypointNum:
if( currentWaypointTransform==null )
{
// grab our transform reference from the waypoint
// controller
currentWaypointTransform=
myWayControl.GetWaypoint( currentWaypointNum );
}
Calculating the distance between two transforms will take into account x , y , and z
coordinates. For the examples in this topic, waypoint following needs only to work on
a two-dimensional plane and the y -axis may be done away with completely. Doing so
can solve problems with ground-based games where the height combined with the x - and
y -axis differences end up not advancing the current waypoint at the right times. If you
were making a game based in the air, you would most likely need to comment this part
out and use x , y , and z .
Both myPosition, a cached Vector3 of the bot's position, and nodePosition, the cached
waypoint position, are stripped of the y -axis in the code below:
// now we check to see if we are close enough to the current
// waypoint to advance on to the next one
myPosition= myTransform.position;
myPosition.y= 0;
// get waypoint position and 'flatten' it
nodePosition= currentWaypointTransform.position;
nodePosition.y= 0;
The distance between the AI bot and the next waypoint is calculated using the two
cached position variables below. When the resulting currentWayDist value is less than the
value of waypointDistance, it means that the bot is close enough to the current waypoint
to advance to the next one:
currentWayDist= Vector3.Distance( nodePosition,myPosition );
if ( currentWayDist < waypointDistance ) {
// we are close to the current node, so let's move
// on to the next one!
Search WWH ::




Custom Search