- Home /
 
How do you set up navmesh agents to go to multiple destinations?
I looked at some tutorials and the documentation on how NavMeshAgents work, and currently have NPC's going to a target in my scene. I can't seem to find where the documentation tells me how to setup multiple destinations for my NPC though. Here is my code for the NavMesh with one target:
 public var target : Transform;
 
 function Start () {
     GetComponent(NavMeshAgent).destination = target.position;
 }
 
               Now here is what I thought up in order to do multiple positions:
 public var currentTarget : Transform;
 public var targets : Transform[];
 public var navigation : NavMeshAgent;
 public var i : int = 0;
 
 
 function Start () {
     
     navigation.destination = targets[i].position;
 
 }
 
 function Update () {
     var dist = Vector3.Distance(targets[i].position,transform.position);
     currentTarget = targets[i];
     
     
     
     //if npc reaches its destination...
     if (dist < 5){
         //go to next target by setting it as the new destination
         navigation.destination = navigation.nextPosition;
         if (i < targets.Length){
             //change next target
             i++;
             navigation.nextPosition = targets[i].position;
         }
     }
     
 
 }
 
               At the moment the NPC is warping all the way down to the last transform in targets[], so I think it's because "i" is getting incremented too fast? How should I fix this?
Answer by Polinator · Mar 20, 2013 at 05:49 PM
I figured it out a fix for myself, but it doesn't use nextPosition, so if anyone would like to chime in and explain how I would exactly use that, it'd be great.
Here's my fix, everything works fine:
 public var currentTarget : Transform;
 public var targets : Transform[];
 public var navigation : NavMeshAgent;
 private var i : int = 0;
 
 
 function Start () {
     
     navigation.destination = targets[i].position;
 
 }
 
 function Update () {
     var dist = Vector3.Distance(targets[i].position,transform.position);
     currentTarget = targets[i];
     
     
     
     //if npc reaches its destination (or gets close)...
     if (dist < 5){        
         if (i < targets.Length - 1){ //negate targets[0], since it's already set in destination.
             i++; //change next target
             navigation.destination = targets[i].position; //go to next target by setting it as the new destination
         }
     }
     
 
 }
 
              Thanks. $$anonymous$$odified to cycle through targets, to "loop" the NPC thingy visiting all different points. Could potentially be modified to find random targets.
Answer by srmojuze · May 18, 2015 at 03:22 PM
Hi, to cycle between destinations:
 #pragma strict
 
 
 private var targets : GameObject[];
 private var navigation : NavMeshAgent;
 private var i : int = 0;
      
      
 function Start () 
       {
         navigation = GetComponent(NavMeshAgent);
          targets = GameObject.FindGameObjectsWithTag("NavTarget");
         
          //set first target
         navigation.destination = targets[i].transform.position;     
          }
      
 function Update () 
          {
          var dist = Vector3.Distance(targets[i].transform.position,transform.position);
          //currentTarget = targets[i].transform;
          //if npc reaches its destination (or gets close)...
          if (dist < 2)
          {                
                 i++; //change next target      
                 if (i < targets.Length )
                  { 
                 navigation.destination = targets[i].transform.position; //go to next target by setting it as the new destination
                  }
               
              //check if at end of cycle, then reset to beginning of cycle
          if (i == targets.Length )
                  {
                  Debug.Log("NAVIGATION FINISHED. RESET.");
                  i = 0;
                  navigation.destination = targets[i].transform.position;
                  }
         }
 }
 
              Answer by demented_hedgehog · Aug 10, 2015 at 01:33 AM
Link to the docs... (I imagine that these docs are newer than the question). http://docs.unity3d.com/Manual/nav-AgentPatrol.html
Your answer