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*.position;*

}

function Update () {
_ var dist = Vector3.Distance(targets*.position,transform.position);_
_ currentTarget = targets;*_

* //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*.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?

3 Answers

3

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*.position;*

}

function Update () {
_ var dist = Vector3.Distance(targets*.position,transform.position);_
_ currentTarget = targets;*_

* //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*.position; //go to next target by setting it as the new destination*
* }
}*_

}

Yeh,,,Thanks Buddy, Its Really useful for me..

Sweet, thanks for posting this.

Thanks. Modified to cycle through targets, to "loop" the NPC thingy visiting all different points. Could potentially be modified to find random targets.

Link to the docs… (I imagine that these docs are newer than the question).

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*.transform.position;* 
  • }*

function Update ()

  • {*
    var dist = Vector3.Distance(targets*.transform.position,transform.position);*
    //currentTarget = targets*.transform;*
    //if npc reaches its destination (or gets close)…
    if (dist < 2)
    *{ *
    i++; //change next target
    if (i < targets.Length )
    * {*
    navigation.destination = targets*.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*.transform.position;
}
}
}*_