Announcement

Collapse
No announcement yet.

[Code (New Bot Added)] Simple Random Movement AI

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [Code (New Bot Added)] Simple Random Movement AI

    ey up

    heres another simple ai for use as a base or a starting point.
    its different from my Deadly Zombie in the way it has an 'active brain' that makes it walk around randomly and does not need to reach its destination.
    you could easily replace the random destination with the player pawn, i can post that ai too if you want. (a few posts below)

    Code:
    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
    {
    
    }
    have fun

    edit:
    heres one that will walk around all random like untill it sees the player close by, then it will follow and shoot. when the player gets too far away it will continue to wander. its actually based on this and this.
    Code:
    class AIWanderer extends UDKBot;
    
    var Vector MyTarget;
    var vector InFront;
    var vector X,Y,Z;
    var vector HitLoc, HitNormal;
    var Actor	HitActor;
    
    var Pawn thePlayer; //variable to hold the target pawn
    
    simulated event PostBeginPlay()
    {
    	super.PostBeginPlay();
    
            //start the brain going at 1 second intervals
       SetTimer(0.1, true, 'BrainTimer');
    }
    
    function BrainTimer()
    {
       local Pawn P; //your player pawn class
       local float Distance;
    
           //////////////////
           //use colliding actors
           foreach WorldInfo.AllPawns(class'Pawn', P)
    	{
              if (P != None)   //if theres a player pawn
              {
                //get the distance
                Distance = VSize2D(Pawn.Location - P.Location);
    
                  //if its close
                if (Distance <= 600)
                {
                      if (CanSee(P))
                   {
                      //look at it and shoot it
                      //Pawn.SetDesiredRotation(Rotator(Normal(P.Location)));
                      Pawn.StartFire(0);
    
    
                   //make the pawn our target
                   MyTarget = P.Location;
                   //go to it
                   GoToState('MoveAbout');
                   }
                   else  //cant see it so wander
                   {
                    Pawn.StopFire(0);
                    WanderAround();
                   }
    
                }
                else  //its too far so wander
                {
                 Pawn.StopFire(0);
                 WanderAround();
                }
              }
            }
    }
    
    function WanderAround()
    {
       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
    {
    
    }

    #2
    not got time to play at the moment,but thanks in advance.as im about 95% modeller and 5% coder,your scripts help bring life to my games.

    Comment


      #3
      hi tegleg,

      You can improve this function so that the bot takes into account movement speed and wont try turning sharply while it is running.

      The way i got taught is to draw a circle of an arbitrary size at a position infront of the pawn relative to its speed. (i.e. pawns location + pawns velocity vector should give you a point infront of the pawn at a distance related to the pawn's velocity.)

      then you take a random point on the circumference of that circle and use that as the move to position.

      The advantage is that when the bot slows down, or stops and exists within the circle, he can rotate 360 deg, but when hes moving the circle will only allow a narrow feild for movement.

      Anyway thought you might be interested in that. I have been learning about swarm intelligence lately but havent implemented anything in UDK yet. Only in opensim :S

      -Mega

      Comment


        #4
        hey that sounds good meganaut.
        you are welcome to post that on here if you get it working.

        i try to make these ai's as simple as possible so that people who are new to scripting have somewhere to start.
        were all still waiting to find out what this AITree is...

        Comment


          #5
          i fixed that first one and updated the post

          heres one that will move around randomly untill the player is near, then it will follow. its a much better at following than deadly zombie

          Code:
          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
          {
          
          }

          Comment


            #6
            This makes me want to try to code a fully boid like bot code i might give it a try in a few days time.

            -Mega

            Comment


              #7
              How do I get my pawn to use this ai controller.

              Comment


                #8
                spawn it in kismet with an ActorFactory http://forums.epicgames.com/threads/...bots-in-kismet
                or put this in the pawn defaultproperties
                Code:
                ControllerClass=class'AIRandom2'
                	bDontPossess=false

                Comment


                  #9
                  Wait... it doesn't work

                  HELP PLEASE.

                  I am using your TestZombiePawn and August 2011 build of UDK.

                  MY PAWN DOES NOT MOVE! Tried Kismet and code thing.

                  Comment


                    #10
                    yeah it doesn't work

                    i have question:
                    how we can tell the pawn use our weapon and fire to player in specific distance like weapon fire range and when player move fallow him to see and start fire again....
                    thank you ....

                    Comment


                      #11
                      it totaly does work.
                      did you get the latest update for the zombie pawn? cos epics been messing around with the base classes again

                      i did a helicopter ai that fires at the player, have a look at that.

                      Comment


                        #12
                        I'll download September build but seeing as I am using August build I didn't think that it would make much of a difference. Well I'll try later. Thanks and I'll tell you my results below.

                        Comment


                          #13
                          not sure if I missed something here but what starts the AI moving? I am using the UDK-2011-09 build of udk.

                          I am using the reposted code like this
                          Code:
                          //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
                          {
                          
                          }

                          Comment


                            #14
                            If it's not working for you try adding the line Pawn.SetMovementPhysics(); to your MoveAbout state, like this:

                            Code:
                            state MoveAbout
                            {
                            Begin:
                                Pawn.SetMovementPhysics();
                                MoveTo(MyTarget);
                            }
                            It helped me to get this working

                            Comment


                              #15
                              Originally posted by DJMidknight View Post
                              not sure if I missed something here but what starts the AI moving?
                              PostBeginPlay() is called automaticaly at the start of the level. in there SetTimer() triggers the BrainTimer on a loop. its fired every 0.5 seconds.
                              in the braintimer function,
                              first it checks if there is another pawn close by, if there is one, the pawn becomes MyTarget and it goes to the state MoveAbout, where it MoveTo() MyTarget.
                              if there is no pawn close by it goes to the MoveRandom state.
                              in the MoveRandom state it generates a random location some distance away and MoveTo() it.

                              hope that makes it a bit more clear

                              Comment

                              Working...
                              X