UnrealScript Game Programming Cookbook

(Chris Devlin) #1
Chapter 5

139

* This is where all of the logic occurs
=======================================================*/
simulated function PathFind()
{
local int Distance;

/** Distance between our pawn's current location and
the next
PathNode in our array */
Distance = VSize2D(Pawn.Location -
WayPoints[_PathNode].Location);

if (Distance <= CloseEnough)
{
// Head towards a random PathNode in our array
_PathNode=Rand(WayPoints.length);
}
GoToState('Pathfinding');
}


  1. Load our Ch5_PathNodes2.udk map and in your Kismet Actor Factory, set your
    controller and pawn to RandomNodeBot and TutorialPawn classes, as this tells
    the factory to now spawn those. You can do this by selecting the pull down, which
    will list the various classes available to us.

  2. Press the PIE button and play in the editor. Your bot should now patrol from node
    to node!


How it works...


This was a very straightforward recipe, as we were only required to make changes to one
function for our new functionality. Making use of UDK's Rand() function allowed us to
pick a random PathNode along the array we created when the bot was spawned.


Allowing a pawn to randomly patrol a map with NavMeshes


We've been working with bots that operate on maps with PathNodes lately. Sometimes
we may find that NavMeshes better suit our needs. In that case, we'll need a bot who can
wander around a map while pathfinding. This prevents the bot from running into walls and
objects along the way.


If the bot does collide with something along its journey, it will pick a different path.

Free download pdf