UnrealScript Game Programming Cookbook

(Chris Devlin) #1

AI and Navigation


140

Getting ready


Open your IDE and prepare to create a new class.


How to do it...


This recipe will be slightly different from our most recent one. Again, we're trying to take
advantage of all that UDK offers in terms of pathfinding, so we'll be creating a bot which
randomly patrols a map, but uses NavMeshes. NavMeshes are probably more common
in UDK development at this point, due to their ease of use and flexibility, so this recipe can
prove invaluable in future AI endeavors.



  1. Create a new class called PatrollingNavMeshBot, and have it extend from
    AIController.
    class PatrollingNavMeshBot extends AIController;

  2. We'll only need two variables here, one to store our temporary destination,
    and another to store our final one.
    var Vector TempDest;
    var vector FinalDest;

  3. As usual, we'll need to add the Possess() function.
    /*=======================================================

    • Forces the pawn to begin moving as soon as the map
      loads
      =====================================================*/
      event Possess(Pawn inPawn, bool bVehicleTransition)
      {
      super.Possess(inPawn, bVehicleTransition);
      Pawn.SetMovementPhysics();
      }



  4. Our PatrolNavMesh state is where all of the functionality occurs in this class.
    We set it to ignore SeePlayer so that it doesn't pay attention to any other pawns on
    the map. The function FineNavMeshPath() is where we tell the bot to clear any
    paths it may have previously had, along with any constraints. Afterwards, we create
    constraints, EnforceTwoWayEdges and FindRandom, which prevent our bot from
    getting stuck, and also allow it to find a random path to our goal.
    Finally, we tell it to find a path to its destination with the FindPath() function.
    FindNavMeshPath is only called once we begin our state. Once it does begin,
    we define a random point on the map, draw a debug line to the point, in addition
    to a red sphere. This allows us to easily view what's going through our bot's mind.

Free download pdf