- Home /
Question by
wagner-felix · Oct 22, 2015 at 03:25 PM ·
waypointnpcwaypoints
NPC Waypoint Choice
I have about 20 NPC's that should each go to exactly one waypoint. There are about 55 possible waypoints.
public class Runtime : MonoBehaviour {
public GameObject[] points;
public GameObject[] npcs;
private int destPoint = 0;
void Start () {
// First Get all available Agents that are tagged as NPC
// Also get all the Waypoints that are tagged as Waypoint
npcs = GameObject.FindGameObjectsWithTag ("NPC");
points = GameObject.FindGameObjectsWithTag ("Waypoint");
// First we can assume that all Agents are placed randomly
// in our scene therefore the first job is for all Agents
// to go to one Random Waypoint
GotoNextPoint();
}
public void GotoNextPoint(){ //GameObject npc) {
// foreach obviously also possible !
for (int i=0; i < npcs.Length; i++){
// If there are no Waypoints exit
if (points.Length == 0)
return;
npcs[i].GetComponent<NavMeshAgent> ().destination = points [destPoint].GetComponent<Transform>().position;
destPoint = Random.Range (0, points.Length);
}
}
Currently
destPoint
chooses a random Waypoint for each NPC that is basically okay however destPoint of NPC1 should not equal destPoint of NPC2 or 3 or ... how do I "generate" a new destPoint for already chosen destPoints. I tried with a while loop where I add a destPoint to a destPoints list and then check destPoint against destPoints. However the loop crashes Unity. Any assistance is greatly appreciated.
Comment
Your answer