Search Unity

Resolved Can't tell when my agent is stopping.

Discussion in 'Navigation' started by maxime66410, Jul 8, 2021.

  1. maxime66410

    maxime66410

    Joined:
    Mar 16, 2018
    Posts:
    19
    Hello, for a few days I have been stuck on a fairly recurring problem that I usually get solved, but the impossible to calculate or to know when the agent arrives at destination.

    It's been several days since I found plenty of solutions that I had already tested, none worked, at least correctly.

    Correctly?
    Yes because at times it gave me the opposite result.
    You are going to tell me bah changes from "<=", but even trying the opposite result, nothing happens.

    Code (CSharp):
    1. if (IsFriend)
    2.                     {
    3.                         if (currentLocation != null)
    4.                         {
    5.                             if (currentLocation.gameObject.GetComponent<Nodes>().IsCaptured == false)
    6.                             {
    7.                                 agent.SetDestination(currentLocation.transform.position);
    8.                                 agent.speed = 2.0f;
    9.                                 agent.stoppingDistance = 2.0f;
    10.                                  
    11.  
    12.                                  // What condition to put here ???????????
    13.                             }
    14.                             else
    15.                             {
    16.                                 if (TimeToWait >= ResultWait)
    17.                                 {
    18.                                     currentLocation = AIManager.Nodes[Random.Range(0, AIManager.Nodes.Length)];
    19.                                 }
    20.                             }
    21.                         }
    22.                         else
    23.                         {
    24.                             AnimatorOfIA.SetBool("IsMove", false);
    25.                             target = null;
    26.                             agent.stoppingDistance = 0.0f;
    27.                             this.gameObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
    28.                             agent.speed = 0.0f;
    29.                             AnimatorOfIA.SetFloat("XRun", 0.0f);
    30.                             AnimatorOfIA.SetFloat("YRun", 0.0f);
    31.                         }
    32.                     }
    Here is the piece of code concerns, everything else works perfectly and does not differ the results I want to get.

    I had already used all these solutions that I made or found on forums having the same problem as me, here are the solutions found that did not work on my side even on a new blank project with a new very simple code (While this solution worked with my friends).

    Solution code A :
    Code (CSharp):
    1. float distance = Vector3.Distance(target.position, transform.position);
    2.                 if (distance <= lookRadiusDetect)
    3.                 {
    4.                        //Insert code
    5.                 }
    Solution code B :
    Code (CSharp):
    1. loat dist=agent.remainingDistance;
    2.  
    3. if (dist!=Mathf.infinite && agent.pathStatus==NavMeshPathStatus.completed && agent.remainingDistance==0)
    4. {
    5.    //Insert code
    6. }
    Solution code C :
    Code (CSharp):
    1.  if (!mNavMeshAgent.pathPending)
    2. {
    3.      if (mNavMeshAgent.remainingDistance <= mNavMeshAgent.stoppingDistance)
    4.      {
    5.          if (!mNavMeshAgent.hasPath || mNavMeshAgent.velocity.sqrMagnitude == 0f)
    6.          {
    7.              // Insert code
    8.          }
    9.      }
    10. }
    Solution code D :
    Code (CSharp):
    1. if ( Vector3.Distance( m_NavAgent.destination, m_NavAgent.transform.position) <= m_NavAgent.stoppingDistance)
    2.          {
    3.              if (!m_NavAgent.hasPath || m_NavAgent.velocity.sqrMagnitude == 0f)
    4.              {
    5.  
    6.              }
    7.          }
    8.      }
    Solution code E :
    Code (CSharp):
    1. if (navpath.status == NavMeshPathStatus.PathInvalid || navpath.status == NavMeshPathStatus.PathPartial) {
    2.    // Target is unreachable
    3. }
    Solution code F :
    Code (CSharp):
    1. if (Vector3.Distance (agent.position, target.position) <= nav.stoppingDistance) {
    2.     // Target reached
    3. }
    Solution code G :
    Code (CSharp):
    1. NavMeshAgent.path.status == NavMeshPathStatus.PathComplete
    Solution code H :
    Code (CSharp):
    1. if(myNavAgent.pathStatus != NavMeshPathStatus.PathComplete) {
    2.   // Do stuff
    3. }
    None of all of this code worked on my end.

    I hope you can help me and teach me more if I make mistakes.