Search Unity

Question Navmesh agent stops moving but has a path

Discussion in 'Navigation' started by Maieshi, Jan 21, 2021.

  1. Maieshi

    Maieshi

    Joined:
    Mar 6, 2019
    Posts:
    1
    I have a navmesh agent to which the control script is attached
    Code (CSharp):
    1.  void Update()
    2.     {
    3.        
    4.         if(Input.GetMouseButtonDown(0))
    5.         {
    6.             playerCharacter.endAttack();
    7.             RaycastHit hit;
    8.             anim.SetBool("Attack", false);
    9.             if (Physics.Raycast(MainCamera.ScreenPointToRay(Input.mousePosition), out hit))
    10.             {
    11.                 Attacking = false;
    12.  
    13.  
    14.  
    15.                
    16.                
    17.                
    18.                // anim.SetBool("Walk", true);
    19.                
    20.                 target = hit.collider.gameObject;
    21.                 if (target?.GetComponent<EnemyCharacter>())
    22.                 {
    23.                     playerCharacter.endAttack();
    24.                     playerCharacter.weapon.EnemyCharacter = null;
    25.                     playerCharacter.weapon.EnemyCharacter = target.GetComponent<EnemyCharacter>();
    26.  
    27.                     playerCharacter.weapon.EnemyCharacter.OnDeath += EnemyOnDeath;
    28.                     CanInteract = true;
    29.                     agent.SetDestination(target.transform.position);
    30.                 }
    31.                 else
    32.                 {
    33.                     agent.SetDestination(hit.point);
    34.                     CanInteract = false;
    35.                 }
    36.  
    37.              
    38.             }
    39.          
    40.         }
    41.        
    42.  
    43.         if (agent.hasPath|| agent.remainingDistance > agent.stoppingDistance) Stopped = false;
    44.         else Stopped = true;
    45.  
    46.         if(Stopped)
    47.         {
    48.             if(CanInteract)
    49.             {
    50.                 anim.SetBool("Walk", false);
    51.  
    52.                 anim.SetBool("Attack", true);
    53.  
    54.                 playerCharacter.startAttack();
    55.  
    56.                 Attacking = true;
    57.             }
    58.             else
    59.             {
    60.                 anim.SetBool("Walk", false);
    61.                 anim.SetBool("Attack", false);
    62.             }
    63.         }
    64.         else
    65.         {
    66.             anim.SetBool("Walk",true);
    67.             anim.SetBool("Attack", false);
    68.         }
    69.  
    70.  
    71.         Debug.Log(/*Stopped+"|||"+CanInteract agent.hasPath*/agent.pathPending +"\\"+ agent.hasPath);
    72.     }
    73.  
    Here I check to see if the agent has a path and then write the appropriate value to the stopped variable, and then use that variable to control the animation of the character. The problem is that when stopping distance> 1.5, the agent reaches this distance and starts running on the spot. at the same time, the agent still has a path and because of this, the running animation continues to play
    https://i.postimg.cc/50Gs573V/image.png
    Безымянный.png
    The red dot indicates the destination path. Maybe somebody knows what the problem is?