I’ve created a fairly simple A* Pathfinding script, but I have encountered one problem. The pathfinder will try and take the shortest path, only to find that there’s a one node gap between it and the target node and it keeps doubling back on itself. So it’ll move up right next to the target node, but all the nodes leading around the pathfinder are all blocked (including the one in-between the pathfinder and the target node) and the only way out is the way it came. But as soon as it moves back the way it came, it tries to re-do what it just did, and then creates this loop where it just moves back & forth. I have no idea how to avoid this. What should I do?
Here’s some of the code if it’ll help you understand:
void CheckNode () {
float lowestF = Mathf.Infinity;
Transform nextNode = null;
int a = 0;
while(a < open.Count){
List<Transform> thisOpen = FindAdjacentNodes(open[a]);
bool addWeight = false;
float weight = 0;
foreach(Transform n in thisOpen){
if(!open.Contains(n) && !addWeight){
addWeight = false;
weight = 0;
}else{
addWeight = true;
weight = errorWeight;
}
}
if((((current.position - open[a].position).magnitude + (finish.position - open[a].position).magnitude) + weight) < lowestF){
nextNode = open[a];
lowestF = (current.position - open[a].position).magnitude + (finish.position - open[a].position).magnitude;
}
a++;
}
if(!closed.Contains(current)){
closed.Add(current);
}
current = nextNode;
if(!closed.Contains(current)){
closed.Add(current);
}
lastOpen = open;
open = FindAdjacentNodes(current);
if(current){
int b = 0;
while(b < nodes.Count){
int c = 0;
while(c < nodes**.nodeVectors.Count){**
** foreach(Transform n in lastOpen){**
__ if(n == nodes**.nodeVectors**__
```c
){
nodes.nodeVectors.Remove(nodes**.nodeVectors[c]);
}
}
c++;
}
b++;
}
closed.Clear();
open.Clear();
closed.Add(start);
current = start;
}
current.renderer.material = green;
}
EDIT
So the above problem has been solved, but the pathfinding itself still gives me a weird path. I started this thread here if maybe you could help me out.
```