Tegleg Records

Tegleg Records
Tegleg Records

Sunday 5 June 2011

UDK Mouse Robot Style AI

a very basic mouse style ai.
it navigates its way round a map without using pathnodes or navmesh.
it traces directly in front to see if theres anything in the way, if theres nothing it moves forward.
if theres an obstruction in front, it takes a random distance left or right and does another trace. if theres an obstruction it does it again, if not it moves there.

heres a vid

heres the code
class AIMouse extends GameAIController;

var Vector MyTarget;
var vector InFront;
var vector X,Y,Z;
var vector HitLoc, HitNormal;
var Actor HitActor;

simulated event PostBeginPlay()
{
 super.PostBeginPlay();

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

function BrainTimer()
{
  GetAxes(Pawn.Rotation, X,Y,Z);

  InFront = Pawn.Location + 200 * X;

  //trace in front
  HitActor = Trace(HitLoc, HitNormal, InFront, Pawn.Location);
  //DrawDebugSphere( HitLoc, 30, 10, 0, 255, 0 );

  if (HitActor != None) //theres something in front
  {
  //trace randomly left or right
     TraceRandom();

  }
  else  //theres nothing in front
  {
  //move forward
    MyTarget = InFront;
    GoToState('MoveAbout');
  }

}

function TraceRandom()
{
  local int LeftRight;

  //make a random number
  LeftRight = Rand(200) - Rand(200);

  GetAxes(Pawn.Rotation, X,Y,Z);

  InFront = Pawn.Location + LeftRight * Y;

     //do another trace to a random location left or right
     HitActor = Trace(HitLoc, HitNormal, InFront, Pawn.Location);
     DrawDebugSphere( HitLoc, 30, 10, 255, 0, 0 );

     if (HitActor != None)  //if we trace something
     {
        Return;
     }
     else  //if we trace nothing
     {
     //move there
        MyTarget = InFront;
    GoToState('MoveAbout');
     }
}

state MoveAbout
{
Begin:

    MoveTo(MyTarget);
}

defaultproperties
{

}

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
{

}

udk random movement ai code

heres an ai that moves around randomly

class RandomAI extends GameAIController;

var Vector MyTarget;

simulated event PostBeginPlay()
{
super.PostBeginPlay();

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

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

//make a random offset, some distance away
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;

GoToState('MoveAbout');

}

state MoveAbout
{
Begin:

MoveTo(MyTarget);
}

defaultproperties
{

}