UnrealScript Game Programming Cookbook

(Chris Devlin) #1
Chapter 5

143


  1. The final piece of the puzzle is the default properties block.
    defaultproperties
    {
    // Pawn is a player or a player-bot
    bIsPlayer = true
    }


With all of this in place, load up the Ch5_NavMesh2.udk map. Open up Kismet
and select the Actor Factory that we created earlier. Select the pulldown which allows
you to select your PatrollingNavMeshBot and TutorialPawn as the pawn and
controller to be used.


  1. Press the PIE button and play in the editor. Your bot should now wander around the
    map and draw red spheres around the map while doing so!


How it works...


NavMeshes allow for a more natural movement for AI bots. If the bot finds that it will collide
with something along its journey it will pick a different point to reach.


Most of our bot's logic is handled in one function, in combination with a state. We start
by clearing any paths or constraints our bot may have previously had, then declare some
of our own. In our case, we want our bot to steer clear of edges and find a random path to
move towards.


The bot performs pathfinding on the NavMesh to see if the random path will be blocked by
some sort of geometry, and if it senses that this is true, the bot will return another random
path. Once the bot reaches that point, it draws another path.


Making a pawn follow us around the map with NavMeshes


We started to see the benefit of using NavMeshes with our last recipe, as it allowed our bot
to determine whether or not a destination was reachable before it began to move to it.


Now we're going to take it to the next step, and have our bot follow us around the map.
This time however, when the bot detects that we are too far away, or it can't reach us
directly, it will create a path of its own to reach us.


Getting ready


Open your IDE and prepare to create a new class.

Free download pdf