Tegleg Records

Tegleg Records
Tegleg Records

Friday, 3 June 2011

udk ai, random movement, follow player

heres a udk ai that moves around randomly unless the player is close, then it will follow.

class AIRandom2 extends GameAIController;

//declaring variables here means
//they may be used throughout this script
var Vector MyTarget;

//at the start of the level
simulated event PostBeginPlay()
{
super.PostBeginPlay();

//start the brain going at half second intervals
SetTimer(0.5, true, 'BrainTimer');
}

function BrainTimer()
{
//local variables are only used in this function
local Pawn P;
local float Distance;

//check if theres another pawn, the pawn belongning to this controller isnt counted
foreach WorldInfo.AllPawns(class'Pawn', P)
{
if (P != None)  //if there is one
{

//get the distance
Distance = VSize2D(Pawn.Location - P.Location);

//if its closer than 500
if (Distance <= 500)
            {
             //make its location MyTarget
             MyTarget = P.Location;
             //so it doesnt fly up n down
             MyTarget.Z = P.Location.Z;
             //MoveTo the pawn
             GoToState('MoveAbout');
            }
            else  //if its too far away
            {
            //do whats in the function called MoveRandom()
            MoveRandom();
            }

          }
          else  //if there isnt another pawn
          {
            //call the function MoveRandom()
            MoveRandom();
          }
        }
}

function MoveRandom()
{   
 local int OffsetX;
  local int OffsetY;

  //make a random number
  OffsetX = Rand(500)-Rand(500);
  OffsetY = Rand(500)-Rand(500);

       //some distance left or right and some distance in front or behind
    MyTarget.X = Pawn.Location.X + OffsetX;
    MyTarget.Y = Pawn.Location.Y + OffsetY;
       //so it doesnt fly up n down
       MyTarget.Z = Pawn.Location.Z;

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

state MoveAbout
{
Begin:
    //MoveTo makes it move to a location (vector)
    MoveTo(MyTarget);
}

defaultproperties
{

}

No comments:

Post a Comment

Loading...