Multi Target Path Implementation

Hi I implemented the example path from the scene examples on the asset file and it works perfectly. Now I want to implement a Multi Target Path to it, I saw MultiTargetPath.cs in the pathfinders folder of the asset. I have also read the multi target path example in the API but I still find it hard to follow. Do I extend the MultiTargetPath like the way I extended AIPath? I really do not know where to begin or how to use the MultiTargetPath.cs script since in the example it did not use this script at all.

Can anyone help me with using multi target path? I tried the example but it gives me the error

UnityEngine.Vector3[][]' toSystem.Collections.Generic.List<UnityEngine.Vector3>[]’

My problem is how to make this:

public void OnPathComplete (Path p) {
	path = p;
	currentWaypointIndex = 0;
	targetReached = false;
	canSearchAgain = true;
	
	if (closestOnPathCheck) {
		Vector3 p1 = p.startPoint;
		Vector3 p2 = GetFeetPosition ();
		float magn = Vector3.Distance (p1,p2);
		Vector3 dir = p2-p1;
		dir /= magn;
		int steps = (int)(magn/pickNextWaypointDist);
		for (int i=0;i<steps;i++) {
			CalculateVelocity (p1);
			p1 += dir;
		}

#if DEBUG
Debug.DrawLine (p1,p2,Color.red,1);
#endif
}

	TrySearchPath ();
}

Into something like this:

public void OnPathComplete (Path p) {
    Debug.Log ("Got Callback");
    
    if (p.error) {
        Debug.Log ("Ouch, the path returned an error\

Error: "+p.errorLog);
return;
}

    MultiTargetPath mp = p as MultiTargetPath;
    if (mp == null) {
        Debug.LogError ("The Path was no MultiTargetPath");
        return;
    }
    
    //All paths
    List<Vector3>[] paths = mp.vectorPaths;
    
    for (int i=0;i<paths.Length;i++) {
        //Plotting path i
        List<Vector3> path = paths[i];
        
        if (path == null) {
            Debug.Log ("Path number "+i+" could not be found");
            continue;
        }
        
        for (int j=0;j<path.Count-1;j++) {
            //Plot segment j to j+1 with a nice color got from Pathfinding.Mathfx.IntToColor
            Debug.DrawLine (path[j],path[j+1],Mathfx.IntToColor (i,0.5F));
        }
    }
}

I tried changing this path = p; to this----> MultiTargetPath mp = p as MultiTargetPath;
and

Vector3 p1 = p.startPoint; to this —> Vector3 p1 = mp.vectorPaths[0].startPoint;

But I still cant access the start Point. Can anybody please help me. I been messing around with the A* project for weeks now. If someone could create a sample scene with two paths it will be very helpful. Since the exaple in the documentation is giving me an error.

Hi

Hm… Maybe the names of my scripts aren’t the best. AIPath is a movement script for a character, MultiTargetPath is a path type, i.e it is used to find the best path from a single point to multiple possible target points.

See this example, I am not sure if it was the same one that you read. But this one definitely uses the MultiTargetPath class.
http://arongranberg.com/astar/docs/_multi_target_path_example_8cs-example.php

First of all thank you very much for answering.

Secondly, Yes after days of studying the code I realized that MultiTargetPath was namespaced as pathfinding so I needed not extend it just needed to use it using pathfinding. I tried to implement the example but I have been getting the error on this part List[] paths = mp.vectorPaths; which tells me that vectorPaths contrary to the documentation is not a List[] but a Vector3[][] therefore would give me the error Cannot implicitly convert type UnityEngine.Vector3[][]' toSystem.Collections.Generic.List<UnityEngine.Vector3>[]’

Third, I modified the AIPath script to use the MultiTargetPath and manually made a vector of 2 targets just to test if the seeker would return 2 paths and it worked. For reference I made changed the list data type to Vector[][] as shown below.

`

void Awake () {
seeker = GetComponent();
seeker.pathCallback += OnPathComplete;
}

public void SearchPath () {

Vector3[] endPoints = new Vector3[2];

	endPoints [0] = target1.position;
	endPoints [1] = target2.position;
	
	canSearchAgain = false;

	MultiTargetPath p = new MultiTargetPath (GetFeetPosition(),endPoints,null,null);
	seeker.StartMultiTargetPath (p);

}

public void OnPathComplete (Path p) {

	if (p.error) {
		Debug.Log ("Ouch, the path returned an error\

Error: "+p.errorLog);
return;
}

	MultiTargetPath mp = p as MultiTargetPath;

	if (mp == null) {
		Debug.LogError ("The Path was not a MultiTargetPath");
		return;
	}

	Vector3[][] paths = mp.vectorPaths;

	for (int i=0;i< mp.vectorPaths.Length;i++) {
		//Plotting path i
		Vector3[] path = mp.vectorPaths[i];
		
		if (path == null) {
			Debug.Log ("Path number "+i+" could not be found");
			continue;
		}
		
		for (int j=0;j<path.Length -1;j++) {
			Debug.DrawLine (path[j],path[j+1],Color.blue);
		}
	}

}
`

The only problem I got now is how could I check which path is the shortest path among all the paths found?

oh never mind I solved it by setting the public bool pathsForAll to false.