Search Unity

Issues with NavAgent distance

Discussion in 'Scripting' started by Foestar, Mar 27, 2019.

  1. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
    Howdy all, so I'm back to making AI for my current project and am currently having issues with the NavMeshAgent's. I'm not sure what I'm doing wrong, but when I post for help it's usually something stupid that I am missing so I was hoping someone else may be able to point out.

    So essentially I have a character who is an agent and based off the point in the levels story will walk to certain points for my cutscene. This is done very easily like so:

    Code (CSharp):
    1. if(StoryPoint == 1)
    2.         {
    3.             CurrentCamTarget = AradiaObject;
    4.             AradiaAnimator.SetBool("Idle", false);
    5.             AradiaAnimator.SetBool("Walking", true);
    6.             AradiaAgent.SetDestination(SP.transform.position);
    7.             StoryPoint++;
    8.         }
    Then we check to see if she has reached her point. I did this the first time by checking if the remaining distance was equal to 0 and a bool saying there was an Agent moving. That tactic worked in my last project, but wont seem to work when I duplicate it in this one. So instead I did it like this.

    Code (CSharp):
    1. if(StoryPoint == 2)
    2.         {
    3.             //check to see if we reached out destination
    4.             if (AradiaAgent.remainingDistance < AradiaAgent.stoppingDistance)
    5.             {
    6.                 Debug.Log("She Made it!");
    7.                 Debug.Log(AradiaAgent.remainingDistance);
    8.                 AradiaAnimator.SetBool("Idle", true);
    9.                 AradiaAnimator.SetBool("Walking", false);
    10.                 movingAgents = false;
    11.             }
    12.         }
    However, this doesn't work either. I even changed the < to <= which gives a result of it working but instantly as if the distance is always 0.

    So in conclusion, my agent seems like it's not getting the distance between the two properly but the character does walk to the point which is a good distance away.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Are these in your update method?
    You have Debug statements in there, are they accurate?
    The "if" statement has to be met before it will print or do anything.
     
  3. dontdiedevelop

    dontdiedevelop

    Joined:
    Sep 18, 2018
    Posts:
    68
    check with if(AradiaAgent.remainingDistance <= AradiaAgent.stoppingDistance + 0.2f)
    because the agent stop himself when remaining dist == stoppingdistance that's why agent's remaining distance can't less than stopping distance
     
  4. tcmeric

    tcmeric

    Joined:
    Dec 21, 2016
    Posts:
    190
    After reaching a waypoint, set the distance var to something other an 0. Not sure how your code works, but are you doing the distance check before a new distance is calculated? Two, there is some problems with navmesh distance check if it has many corners. See this thread here for a solution (calculating the true distance by checking corner to corner). https://forum.unity.com/threads/getting-the-distance-in-nav-mesh.315846/
     
  5. tcmeric

    tcmeric

    Joined:
    Dec 21, 2016
    Posts:
    190
    Something like this.

    Code (CSharp):
    1.             NavMeshPath path = agent.path;
    2.  
    3.             Vector3 previousCorner = path.corners[0];
    4.             float lengthSoFar = 0.0F;
    5.             int i = 1;
    6.             while (i < path.corners.Length) {
    7.                 Vector3 currentCorner = path.corners[i];
    8.                 lengthSoFar += Vector3.Distance(previousCorner, currentCorner);
    9.                 previousCorner = currentCorner;
    10.                 i++;
    11.             }
    12.  
    13.             var totalPathDistance = lengthSoFar;
     
  6. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
    So I have played with this extensively and can't figure out why the distance isn't reading the way I want it to. I've added many debugs in and the distance for some reason remains 0 no matter what which is why it keeps finishing before it should. I set a destination, but it reads that distance as zero.

    That being said, I then thought about what fire7side said above.
    They were in an update function, but it was a fixed update. This made a little better as it wouldn't flood the system with as much. Then thinking about that, I instead removed everything out of the fixed update tonight except for a variable that when turned on will make the camera rotate to another objects transform position. Instead I created new void functions for each story point. But even cleaning it up like this I still get the same results. I then went on to try what dontdiedevelop wrote.

    I figured that made more than enough sense. But even then I was still having the wrong results. The distance no matter what I did to try and change it my distance keeps coming out as zero.

    So, I decided to take another approach as I really wanted to get ahead of this point in the development so I could have a working demo. I ended up making my code have a private game object that is the current story point. This object is the destination of the agents. And instead of checking to see if the distance was what I needed at that current point I instead checked to see if the agents position was the same as the story points position. And it worked. Probably not the best method, but was what I needed. I do intend to come back and work with the code a bit more in the near future. I'm just baffled though because I never had a problem before hand with my ai following destinations, and checking remaining distances.