- Home /
A* freezes unity
I'm trying to learn about pathfinding, right now I'm getting unity freezed at second iteration. Here's the part which freezes (it goes fine at the first time) so if you can't spot any problems in this function I'll post some more, maybe problem is somewhere else.
private Node GetByPosition(Vector3 pos, Dictionary<int, Node> d){
Node m = null ;
print("checking");
foreach(KeyValuePair<int, Node> n in d){
if(n.Value.position == pos){
m = n.Value;
}
}
if(m == null)
print ("No Node in dict");
return m;
}
I don't see any problem with that code. If it goes fine the first time you search but not the second, is it possible you aren't clearing the Dictionaries or resetting certain flags in between searches? $$anonymous$$aybe that causes the condition of a loop somewhere never to get satisfied. $$anonymous$$nowing how full the dictionary is when this function gets called, compared to how full you'd expect it to be, would be useful too.
I have added conditional statement to check if dictionary contains any records before going through it, but it doesn't change anything and this method returns null if there's no hits anyway so I'll just post the main loop:
while(open.Count > 0 && failsafe > 0){ //While we have something in open dictionary
yield return new WaitForSeconds(.5f);
failsafe--;
current = Pick(open); //Set current node to best choice from open dictionary
RemoveByValue(open, current); //Remove current from open
Place(closed, current); //and place it in closed
if(current.position == target){
StartCoroutine("GeneratePath", current); //When target is reached, break loop and generate path from the last element
break;
}
GameObject tmp2 = (GameObject)Instantiate(player, current.position, Quaternion.identity);
tmp2.renderer.material.color = Color.yellow;
gScore = current.cost;
foreach(Node n in GetParentNodes(current)){
print ("Open: " + open.Count + " Closed: " + closed.Count + " For: " + current.position.ToString());
if(n != null){
if(obstacles.ContainsValue (n.position) == false){
GameObject tmp = (GameObject)Instantiate(player, n.position, Quaternion.identity);
tmp.renderer.material.color = Color.green;
float estmationOfGscore = current.cost + n.cost;
yield return new WaitForSeconds(.5f);
Destroy(tmp,0);
print("Check n.pos " + n.position);
yield return new WaitForSeconds(.5f);
if(GetByPosition (n.position, closed) != null)
continue;
if(GetByPosition (n.position, open) == null && GetByPosition (n.position, closed) == null){
//print ("move to open");
n.cost = current.cost + 1;
n.fScore = n.cost + Heuristic(n);
n.previous = current;
Place (open,n);
}
}
}
}
Destroy(tmp2,0);
}
Answer by HappyMoo · Jan 21, 2014 at 10:26 PM
Are you going into an infinite Loop? Debug your program to see why.
Set Breakpoints in MonoDevelop and Run-Menu -> AttachToProcess
Thanks mate! That helped me to resolve mystery. I would never expecte that the reason could be trying to place a random number as a key (max random value was a little bit high) if default key already existed when adding new node to dictionary!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
A* Pathfinding Project Y-axis movement, help. 1 Answer
How do I implement A* pathfinding to my 2d game, without tiles? 4 Answers
A* endless loop 0 Answers