UnrealScript Game Programming Cookbook

(Chris Devlin) #1

AI and Navigation


132

The OffsetX and OffsetY variables tell us how far to the left and right, and then
forward and backward, we want our bot to move each time the function is called.
Our target location is where we want to set the bot. We're simply taking our current
pawn's location, and subtracting it from the offset, that is, where we want the bot to
be going. Each time MoveRandom() is called, it will pick a random integer between
the minimum and maximum values you've set.
At the end of this function we're informing the bot to go to the wander state.

/*=======================================================
* Math for our wandering state
=======================================================*/
function MoveRandom()
{
local int OffsetX;
local int OffsetY;

// set a random number for our X value
OffsetX = Rand(1300)-Rand(700);

// set a random number for our Y value
OffsetY = Rand(1100)-Rand(1100);
// distance left or right of the pawn
TargetLocation.X = Pawn.Location.X + OffsetX;
// distance in front of or behind the pawn
TargetLocation.Y = Pawn.Location.Y + OffsetY;
/** prevents the pawn from quickly aiming up and down
the Z axis while moving */
TargetLocation.Z = Pawn.Location.Z;

// move to the random location
GoToState('Wander');
}


  1. The wander state only has one function, and that is MoveTo(TargetLocation).
    The math for the target location (a vector) was previously done in our MoveRandom()
    function.
    /*=====================================================

    • Tells the pawn to start wandering
      =======================================================*/
      state Wander
      {
      Begin:




// move to a location (vector)
MoveTo(TargetLocation);
}
Free download pdf