- Home /
Problem with loop the get better path to goal
Hey everyone, I'm slowly trying to acquire some results with path finding and started reading some articles of A* to get a base. I don't know if I'm in the right path but I've reached this. (if you guys want the whole code I can post it here, i just didn't because I coded it in portuguese and take me a lot to translate).
The problem in the function below is that it's breaking Unity. Probably because of the loop.
What 'hasClearPath' does is basically launch a Linecast from the position of the last node of the list to the goal.
public void findBestPath() {
closedList.Clear();
betterPath.Clear();
openList = Nodes;
betterPath.Add( findFirstNode() );
while(true) {
List<Nodes> Neighbors = findNeighbors( betterPath[betterPath.Count - 1] ); // returns a list of neighbors of the last item in betterPath
betterPath.Add( findBetterNeighbor( Neighbors ) ); // find the better neighbor( lowest F) between Neighbors returned above and add him to the list
if(betterPath[betterPath.Count - 1].hasClearPathTo(Goal)) { // if the last item of betterPath has clear path to the goal, breaks the loop
break;
}
}
}
Any hint on this?
Thanks from now!
Answer by Berenger · May 12, 2012 at 05:34 AM
Well, the while true is not going to do any good there. It makes sense when you use it in a coroutine which is paused every frame or x seconds, but there your code juste get stucked inside the loop. So yield null or find a way to leave the loop at some point.
Yeah. I wasn't sure about the use of while(true). I also tried while(Physics.Linecast(betterPath[betterPath.Count - 1].position, Goal.position)) but I get stucked in the loop too. What's the better way to get out of the loop in this case? I also try to check if the goal node is inside the closedList and then break the loop, but it breaks Unity.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071400im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A* Pathfinding Bugs 0 Answers
A While Loop in a path finding system is freezing, how can I fix this? 0 Answers
Anyone help with an infinite loop problem on a while list.count >0 condition 2 Answers
NavMeshAgent Different Sizes Help 0 Answers
Unity2D Grid A* pathfinding freezing for a few seconds 1 Answer