Search Unity

Movement of NavMeshAgent

Discussion in 'Scripting' started by startas, Nov 19, 2014.

  1. startas

    startas

    Joined:
    Nov 14, 2014
    Posts:
    102
    I have a few questions about nav agent and its movement. I have a nav agent, i have made a little map and i wanna make it move to one target, then to next target and so on. My current code is:

    agent = GameObject.Find("MyAgent").GetComponent(NavMeshAgent);
    target = GameObject.Find("TargetsName").GetComponent(Transform);

    And then i make it move to target with:
    agent.SetDestination(target.position);

    1) how do make sure, that agent arrived to its currently set destination ? Is there a realable function for that ?
    2) after that, how to make it to move to the next target ? i tried some variants, but i dont know if it failed to determine its arrival to the first target or it failed to move to the next target.

    I tried looking for some functions, but havent found anything that would work, i even found on google some crazy quadra checks for question 1, but it didnt work either.
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    navmeshagent has remaining distance. This should be sufficient enough to determine if you have reached teh end.

    steeringtarget is the current target point in the path, so you can test distance to this to determine if you have reached teh current waypoint. Useful if you want to use special code to avoid agents getting stuck on other agents and not being able to reach waypint.

    generally I control all movement of my agents. Once it has calculated a path, I take those points, then send my vehicle on its way, deciding when to use the next point as the target.
     
  3. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
  4. startas

    startas

    Joined:
    Nov 14, 2014
    Posts:
    102
    Well, it doesnt work. I wrote a line to print that boolean value of agent.hasPath every frame, and what happens is that this value is true till agent reaches the target, then i change agent destination with setDestination method, and then haspath returns both true/false values every second frame, and my agent doesnt move to the next target.