Graphics Reference
In-Depth Information
if( followTarget!=null )
LookAroundFor( followTarget );
break;
So far, the AI code has dealt with patrolling and standing guard, but the script also
supports waypoint-following and -steering behaviors.
The first method of waypoint following is AIState.translate_along_waypoint_path,
which moves the bot along a path without rotation:
case AIState.translate_along_waypoint_path:
// following waypoints (moving toward them, not
// pointing at them) at the speed of moveSpeed
Before trying to follow any sort of path, it is a good idea to check that the initializa-
tion function was completed (didInit will be true) and that the last waypoint has not been
reached. If didInit is false or the last waypoint has been reached, the path-following should
stop and the function returns:
// make sure we have been initialized before trying
// to access waypoints
if( !didInit && !reachedLastWaypoint )
return;
The UpdateWaypoints() function is called to check the distance from the bot to the
waypoint and update which waypoint to follow, as needed:
UpdateWaypoints();
isStationary is a Boolean variable used to provide a method of keeping the AI bot sta-
tionary, if required. When it is set to true, the bot will not be moved. Note that this state
does not have any rotation code or provide input values for its movement; it uses Unity's
Transform.Translate to directly translate the transform toward each waypoint, instead. It
will use the transform held in the variable myTransform:
// move the ship
if( !isStationary )
{
A Vector3 called targetMoveVec provides a vector for the bot to move along, toward
the waypoint. This movement vector is calculated by subtracting the position of the bot
from the current waypoint transform's position. The vector is then normalized with Unity's
Vector3.Normalize function to strip away the magnitude. Note that the waypoint's trans-
form is held in the variable currentWaypointTransform, put there by that earlier call to the
UpdateWaypoints() function:
targetMoveVec = Vector3.Normalize
( currentWaypointTransform.position -
myTransform.position );
The actual amount we move is an interpolated vector somewhere between the cur-
rent value of moveVec (the current amount being moved) and the vector we just calcu-
lated in targetMoveVec. pathSmoothing is a float variable used to determine how long the
Search WWH ::




Custom Search