- Home /
Question by
DayyanSisson · Dec 05, 2012 at 04:51 AM ·
pathfindingdistancewhilenodes
A* Pathfinding Bugs
I'm writing a simple A* pathfinding AI and I've run into a bug: the pathfinder jumps over gaps 1 node wide. So if I have 2 nodes, and then an empty space in-between where they aren't adjacent, it just jumps that gap and moves on to the second node. I tried restricting the nodes it could find by distance but that didn't really affect anything. What am I doing wrong?
public Vector2 pathfindingLayers;
public Vector3[] offsets = new Vector3[8];
public Transform holder;
public List<Transform> nodes;
public List<Transform> open;
public List<Transform> closed;
public Transform current;
public Transform finish;
public Material yellow;
public Material green;
public Material red;
void Start () {
int i = 0;
while(i < holder.childCount){
nodes.Add(holder.GetChild(i));
i++;
}
open = FindAdjacentNodes();
}
void Update () {
if(current != finish){
CheckNode();
}
}
void CheckNode () {
float lowestF = Mathf.Infinity;
Transform nextNode = null;
int a = 0;
while(a < open.Count){
if(((current.position - open[a].position).magnitude + (finish.position - open[a].position).magnitude) < 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);
}
current.renderer.material = green;
open = FindAdjacentNodes();
}
List<Transform> FindAdjacentNodes () {
List<Transform> nodeList = new List<Transform>();
nodeList.AddRange(nodes);
List<Transform> adjacentNodes = new List<Transform>();
int i = 0;
while(i < 8){
float closeDis = Mathf.Infinity;
Transform closest = null;
int a = 0;
while(a < nodeList.Count){
if(nodeList[a] != current){
float distance = (nodeList[a].position - current.position).magnitude;
if(distance < closeDis && nodeList[a].gameObject.layer == pathfindingLayers.x){
closest = nodeList[a];
closeDis = distance;
}
}
a++;
}
adjacentNodes.Add(closest);
nodeList.Remove(closest);
i++;
}
return adjacentNodes;
}
Comment
Your answer
Follow this Question
Related Questions
Alternative to remainingDistance (Entire Path Calculation)? 0 Answers
How to calculate distance based on A*? 2 Answers
Pathfinding in Destruction 0 Answers
How to compare several distances? 1 Answer
Best Way To Create Pathfinding Nodes 1 Answer