Announcement

Collapse
No announcement yet.

[Code] Mouse Style AI (Pathfinding)

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

    [Code] Mouse Style AI (Pathfinding)

    yo

    since the forum messed up and all the threads from the past few days are gone, ill give you this code again.

    it traces in front, if theres no obstruction it moves forward.
    if there is an obstruction it generates a random destination left or right.
    if theres nothing in the way it moves there

    heres a vid
    http://www.youtube.com/watch?v=kzm6I...el_video_title
    the bot here moves in a smoother manner that in the vid because of this line
    SetTimer(0.5, true, 'BrainTimer');
    in the vid its
    SetTimer(1.0, true, 'BrainTimer');


    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(0.5, 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
    {
    
    }
    heve fun
    questions, comments are welcome

    #2
    this one is the same apart from..
    if theres another pawn near by, follow it

    Code:
    class AIMouse2 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(0.5, true, 'BrainTimer');
    }
    
    function BrainTimer()
    {
     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');
                 Return;
                }
                else   //its far away so navigate around the place
                {
                   //move forward
                   GoAhead();
                   Return;
                }
           }
           else    //theres no pawn
           {
             //move forward
             GoAhead();
             Return;
           }
        }
    }
    
    function GoAhead()
    {
       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();
                     Return;
                     }
                     else  //theres nothing in front
                     {
                     //move forward
                     MyTarget = InFront;
                     GoToState('MoveAbout');
                     Return;
                     }
    }
    
    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');
        Return;
         }
    }
    
    state MoveAbout
    {
    Begin:
    
        MoveTo(MyTarget);
    }
    
    defaultproperties
    {
    
    }

    Comment


      #3
      Thanks..

      Comment


        #4
        hello, my zombie not attack,and no walk,they stay and idle.
        class SAPawnZombie extends SABaseEnemy

        Placeable;

        var Pawn P; // variable to hold the pawn we bump into

        // members for the custom mesh
        var SkeletalMesh defaultMesh;
        var MaterialInterface defaultMaterial0;
        var AnimTree defaultAnimTree;
        var array<AnimSet> defaultAnimSet;
        var AnimNodeSequence defaultAnimSeq;
        var PhysicsAsset defaultPhysicsAsset;

        simulated function SetCharacterClassFromInfo(class<UTFamilyInfo> Info)
        {
        Mesh.SetSkeletalMesh(defaultMesh);
        Mesh.SetMaterial(0,defaultMaterial0);
        Mesh.SetPhysicsAsset(defaultPhysicsAsset);
        Mesh.AnimSets=defaultAnimSet;
        Mesh.SetAnimTreeTemplate(defaultAnimTree);

        }

        simulated event Bump( Actor Other, PrimitiveComponent OtherComp, Vector HitNormal )
        {
        `Log("Bump");

        Super.Bump( Other, OtherComp, HitNormal );

        if ( (Other == None) || Other.bStatic )
        return;

        P = Pawn(Other); //the pawn we might have bumped into

        if ( P != None) //if we hit a pawn
        {
        if (P.Health >1) //as long as pawns health is more than 1
        {
        P.Health --; // eat brains! mmmmm
        }
        }
        }

        defaultproperties
        {


        Begin Object Name=WPawnSkeletalMeshComponent
        AnimTreeTemplate=AnimTree'All.zs.zs_animtree'
        SkeletalMesh=SkeletalMesh'All.zs.SZ'
        AnimSets(0)=AnimSet'All.zs.ZS_animset'
        PhysicsAsset=PhysicsAsset'All.zs.ZS_Physics'
        Scale3D = (X=1.7,Y=1.7,Z=1.7)
        Translation = (Z=-90)
        End Object

        Begin Object Name=CollisionCylinder
        CollisionRadius=+0042.000000
        CollisionHeight=+0088.000000
        HiddenGame=false
        End Object

        RagdollLifespan=180.0

        ControllerClass=class'SAZombieAI'

        Health=50
        GroundSpeed=300

        bDontPossess=false
        //animation properties
        WalkAnim=zs_run
        AttackAnim=zs_attack
        IdleAnim=zs_walk
        WalkAnimDuration = 1.03
        AttackAnimDuration = 1.03
        IdleAnimDuration = 3.03
        }
        class SAZombieAI 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(0.5, true, 'BrainTimer');
        }

        function BrainTimer()
        {
        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');
        Return;
        }
        else //its far away so navigate around the place
        {
        //move forward
        GoAhead();
        Return;
        }
        }
        else //theres no pawn
        {
        //move forward
        GoAhead();
        Return;
        }
        }
        }

        function GoAhead()
        {
        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();
        Return;
        }
        else //theres nothing in front
        {
        //move forward
        MyTarget = InFront;
        GoToState('MoveAbout');
        Return;
        }
        }

        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');
        Return;
        }
        }

        state MoveAbout
        {
        Begin:

        MoveTo(MyTarget);
        }

        defaultproperties
        {

        }

        Comment


          #5
          @Karpi1989
          please dont spam everywhere with this, do a bit of searching its been answered many times
          http://forums.epicgames.com/threads/963891-no-anim

          Comment


            #6
            is not code from other post, is your code

            i don know where paste code
            SAPawn(Owner).FullBodyAnimSlot.PlayCustomAnim('zs_ attack', 2.0, 1.0, 0.0, false, false);

            Comment


              #7
              but its about the exact same thing
              read my Deadly Zombie thread, perhaps the answer lies there?

              Comment

              Working...
              X