UnrealScript Game Programming Cookbook

(Chris Devlin) #1

AI and Navigation


146

Now it's time to create our follow state. We're going to write the function for
FindNavMeshPath() first, which is nearly identical to the one we used in our
previous recipe. However, we are creating new constraints here. We're passing
in our target variable, which was defined previously in our idle state, as the pawn
our bot has just seen. We then tell the bot to find the most appropriate path to our
pawn by returning the NavigationHandle.FindPath function.
/*=======================================================
* Nav Mesh code for following our pan
======================================================*/
state Follow
{
ignoresSeePlayer;
functionboolFindNavMeshPath()
{

/** Clear cache and constraints (ignore recycling
for the moment) */
NavigationHandle.PathConstraintList = none;
NavigationHandle.PathGoalList = none;

// Create constraints
class'NavMeshPath_Toward'.static.TowardGoal
(NavigationHandle,target);

class'NavMeshGoal_At'.static.AtActor
(NavigationHandle, target,32);

// Find path to our pawn
return NavigationHandle.FindPath();
}


  1. The second half of our follow state is where the state actually begins.
    If we can reach our pawn directly, then we just move toward it without needing to
    perform any sort of pathfinding. Otherwise, if the bot determines that it can't reach
    our pawn directly then it's time to perform pathfinding. The bot will now create a path
    to its target Actor.
    With the DrawPathCache() function, we draw lines to each point in our path
    cache, from our current location to where our bot is. Afterwards, we move to the first
    node on that path. The two lines of debug code that follow determine the color of our
    lines, as well as the color of the sphere at our next location.
    Once we've reached our pawn, go back to the beginning of the state again. If at any
    point we've determined that we can't reach the pawn, whether through pathfinding
    or not, we go back to our idle state to prevent an infinite loop, which would create a
    game crashing bug.

Free download pdf