- A* version: [5.3.5]
- Unity version: [6000.0.43]
So, first time playing with the asset (pro) and really enjoying it. I have setup a bot with AIPath and the below script. I want that the AIPath/Seeker looks at all the targets, and then builds the shortest path to move to each one in order. Currently it is finding the shortest path and just going to the first target then stopping. Any ideas?
public class MultiTargetPathExample : MonoBehaviour
{
private Seeker seeker;
public Transform[] targets;
void Start()
{
Search();
}
void Search()
{
// Set the target points
Vector3[] endPoints = new Vector3[targets.Length];
for (int i = 0; i < targets.Length; i++) endPoints[i] = targets[i].position;
MultiTargetPath p = MultiTargetPath.Construct(transform.position, endPoints, null, OnPathComplete);
// Find a path only to the closest one, not necessarily all of them
p.pathsForAll = true;
// This is either AIPath, RichAI or AILerp depending on which movement script you are using.
// All of them implement this interface
var ai = GetComponent<IAstarAI>();
// Disable the built-in path recalculation of the movement script
ai.canSearch = false;
// Use the MultiTargetPath instead (the movement script will send it to the Seeker to be calculated)
ai.SetPath(p, true);
}
public void OnPathComplete(Path p)
{
Debug.Log("Got Callback");
if (p.error)
{
Debug.Log("Ouch, the path returned an error\nError: " + 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++)
{
// Plot path i
List<Vector3> path = paths[i];
Debug.Log("pathing is working");
if (path == null)
{
Debug.Log("Path number " + i + " could not be found");
continue;
}
var color = AstarMath.IntToColor(i, 0.5F);
for (int j = 0; j < path.Count - 1; j++)
{
// Plot segment j to j+1 with a nice color got from Pathfinding.AstarMath.IntToColor
Debug.DrawLine(path[j], path[j + 1], color, 10);
}
}
}
}