Obtain multiple paths to target using navmesh
Hi all,
I am new to game development and I am working on a 3D procedural platform level generator.
I autogenerate the navmesh (plus offmeshlinks) with the platform. I am currently using unity navmesh inbuilt CalculatePath to check if there is a path from a start point to the end point. This gives me information on the shortest path available from the start point to the end point. A single path. I also want to be able to determine the number of alternative paths to a given goal.
I have come across arongranberg A* Pathfinding Project (Free version) and it mentions the AlternativePath class .
My question is, I am struggling to see how to use AlternativePath to penalize nodes so that when I call the seeker again, it doesn't choose that same path. Not sure if I understood things properly but it should be able to do this?
I basically want to use the example given in MultiTargetFree.cs but with multipaths instead of multitargets.
I've tried the following but it doesn't seem to work:
using System.Collections.Generic;
using System.Linq;
using Pathfinding;
using UnityEngine;
public class TryGetAllPaths : MonoBehaviour
{
public HashSet<List<Vector3>> allPaths = new HashSet<List<Vector3>>();
public int counter = 5;
private Seeker seeker;
private Path currentPath;
private bool printList = true;
public List<Vector3> path1 = new List<Vector3>();
public List<Vector3> path2 = new List<Vector3>();
public void Awake()
{
seeker = GetComponent<Seeker>();
}
public void Start () {
seeker.StartPath (transform.position, transform.position+transform.forward*10, OnPathComplete);
}
public void Update()
{
while (counter > 0 && currentPath!= null && currentPath.IsDone())
{
currentPath = null;
seeker.StartPath(transform.position, transform.position+transform.forward*10, OnPathComplete);
counter--;
}
if (counter == 0 && allPaths.Count > 2 && printList)
{
List<List<Vector3>> tmp = allPaths.ToList();
path1 = tmp[0];
path2 = tmp[1];
printList = false;
}
}
public void OnPathComplete (Path p) {
//We got our path back
if (p.error) {
// Nooo, a valid path couldn't be found
} else {
currentPath = p;
var altPath = GetComponent<AlternativePath>();
allPaths.Add(p.vectorPath);
// Debug.DrawLine(p.vectorPath[0], p.vectorPath[p.vectorPath.Count - 1], Color.green, 10);
altPath.Apply(p);
}
}
}