Hey, I’ve been trying to add this pathfinding function to my program, but when I run it, it just crashes. I’ve read other threads and this might be an infinite loop, but I don’t see where it would be other than the recursion area (and it shouldn’t break there because of the if-statement checking if it’s out of bounds).
public void SetPath(Vector3 pos, Vector3 goal, Queue<Vector3> p, List<Vector3> visited, int cost) {
Debug.Log("checking " + pos);
//method which checks if the target tile is outside of the map
if (core.OutOfBounds((int)goal.x, (int)goal.z))
Debug.LogError("target tile " + goal + " is out of bounds!");
//if the current position isn't out of bounds and hasn't been visited
else if (!core.OutOfBounds((int)pos.x, (int)pos.z) && !visited.Contains(pos)) {
Debug.Log("pos is within the limits of the map");
//add tile to the visited list and path queue
visited.Add(pos);
p.Enqueue(pos);
//add tile movement cost to total path cost
cost += core.TileCost((int)pos.x, (int)pos.z);
//if the current position is the target tile and the path is shorter than the current one
if (pos == goal && cost < shortestPath) {
Debug.Log("pos is the target tile");
shortestPath = cost;
path = p;
}
//else, check tiles around
else {
SetPath(new Vector3(pos.x + 1, 0, pos.z), goal, p, visited, cost);
SetPath(new Vector3(pos.x, 0, pos.z + 1), goal, p, visited, cost);
SetPath(new Vector3(pos.x - 1, 0, pos.z), goal, p, visited, cost);
SetPath(new Vector3(pos.x, 0, pos.z - 1), goal, p, visited, cost);
}
}
}