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
{

}

No comments:

Post a Comment