Search Unity

Enemy AI Sliding

Discussion in 'Scripting' started by Camronas, Oct 21, 2014.

  1. Camronas

    Camronas

    Joined:
    Apr 20, 2012
    Posts:
    154
    Hello everyone,

    I am making a FSM so my enemies can go between simple movement and path finding.

    When in the BasicWalk state it simply does a look at and moves forward. Every so often it checks its old position and its current position; if there is no significant position change it determines that the enemy is stuck and goes to path finding.

    It flicks between the two fine however when it bounces from AdvancedWalk the enemy slides until it bounces on the wall of the navmesh.

    Code (CSharp):
    1.  
    2. void Idle()
    3.     {
    4.  
    5.         animation.CrossFade(idleClip.name);
    6.  
    7.         attackRechargeTimer -= Time.deltaTime;
    8.         if ( attackRechargeTimer < 0 )
    9.         {
    10.             attackRechargeTimer = maxAttackRechargeTimer;
    11.             hitBool = false;
    12.         }
    13.  
    14.         if(!InRange())
    15.         {
    16.             state = State.BasicWalk;
    17.         }
    18.     }
    19.  
    20.     void BasicWalk()
    21.     {
    22.         animation.CrossFade(walkClip.name);
    23.  
    24.         Vector3 targetPostition = new Vector3( Target.transform.position.x, this.transform.position.y, Target.transform.position.z ) ;
    25.         this.transform.LookAt( targetPostition );
    26.  
    27.         controller.SimpleMove((transform.forward * Speed) *Time.deltaTime);
    28.  
    29.         advancedWalkTimer -= Time.deltaTime;
    30.         if ( advancedWalkTimer < 0 )
    31.         {
    32.  
    33.             _oldPosition = _curPosition;
    34.             _curPosition = transform.position;
    35.  
    36.             advancedWalkTimer = maxAdvancedWalkTimer;
    37.             if(Vector3.Distance(_oldPosition, _curPosition)<advancedWalkRange)
    38.             {
    39.                 state = State.AdvancedWalk;
    40.             }
    41.         }
    42.  
    43.         if(InRange())
    44.         {
    45.             state = State.Idle;
    46.         }
    47.     }
    48.  
    49.     void AdvancedWalk()
    50.     {
    51.         animation.CrossFade(walkClip.name);
    52.  
    53.         navMeshAgent.SetDestination(Target.transform.position);
    54.  
    55.         advancedWalkTimer -= Time.deltaTime;
    56.         if ( advancedWalkTimer < 0 )
    57.         {
    58.             advancedWalkTimer = maxAdvancedWalkTimer;
    59.             navMeshAgent.Stop();
    60.             state = State.Idle;
    61.         }
    62.  
    63.         if(InRange())
    64.         {
    65.             navMeshAgent.Stop();
    66.             state = State.Idle;
    67.         }
    68.     }
    69.  
    Very sorry for the lack of comments but I do hope it is clear enough to see what I am aiming for. Any help would be great! Thanks in advance.
     
  2. Camronas

    Camronas

    Joined:
    Apr 20, 2012
    Posts:
    154
    Just pumping this up.

    Still looking for help, thanks.