• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by bbdude95 · Jan 08, 2015 at 10:43 AM · c#pathfindingastarnodesnode

A* endless loop

My A* path finding loops through a list of nodes that are neighbors of your starting node, however the game doesn't find a path when you are not on the same column as the goal.

 public class Node
 {
     public Node()
     {
     }
     public Vector3 pos;
     public override bool Equals (object obj)
     {
         Node test = (Node)obj;
         bool value = false;
         if (pos.x == test.pos.x &&pos.y == test.pos.y &&pos.z == test.pos.z)
         {
             value = true;
         }
         return value;
     }
 }

my A* class

 public class AstarHolder{
 
     //List<Node> emptySet;
     List<Node> openSet = new List<Node>();
     List<Node> closedSet = new List<Node>();
     Dictionary<Node,Node> cameFrom = new Dictionary<Node, Node>(); //new List<Node>();
     Dictionary<Node,float> gScore = new Dictionary<Node, float>();
     Dictionary<Node,float> fScore = new Dictionary<Node, float>();
     public Dictionary<Node,List<Node>> neighborNodes = new Dictionary<Node, List<Node>>();
     public AstarHolder()
     {
     }
 
     public bool Holds(List<Node> list,Node searchNode)
     {
         foreach(Node node in list)
         {
             if (node.Equals(searchNode))
                 return true;
         }
         return false;
     }
     public bool Holds(Dictionary<Node,Node> list,Node searchNode)
     {
         foreach(Node node in list.Values)
         //foreach()
         {
             if (node.Equals(searchNode))
                 return true;
         }
         return false;
     }
     public float Holds(Dictionary<Node,float> list,Node searchNode)
     {
         foreach(Node node in list.Keys)
         {
             if (node.Equals(searchNode))
                 return list[node];
         }
         return 1000;
     }
 
     float heurCost(Node start, Node goal)
     {
         return Vector3.Distance(start.pos,goal.pos)/4;
     }
     public List<Node> astar(Node start,Node goal)
     {
         //(list)
         closedSet.Clear(); //the set of nodes already evauluated.
         openSet.Add(start); //the set of tentative nodes to be checked
         cameFrom.Clear(); // the map of navigated nodes
         
         gScore[start] = 0;
         fScore[start] = gScore[start] + heurCost(start,goal);
         int it = -1;
         while (openSet.Count > 0 && it < 10000)
         {
             it++;
             //current = //the node in the openset having the lowest fscore[] value
             Node current = new Node();
             foreach (Node newCurrent in cameFrom.Values)
             {
                 current = newCurrent;
             }
             float lowestScore =1000;
             foreach (float fcheck in fScore.Values)
             {
                 if (fcheck < lowestScore)
                 {
                     //lowestNode = fScore.;
                     lowestScore = fcheck;
                 }
             }
             foreach (Node testNode in fScore.Keys)
             {
                 if (lowestScore == fScore[testNode] && !Holds(closedSet,testNode))
                     current = testNode;
             }
 
             if (current == goal || Holds(cameFrom,goal) /*|| cameFrom.Count >= 2*/)
             {
                 return redoPath(cameFrom,goal);
             }
             openSet.Remove(current);
             if (!Holds(closedSet,current))
                 closedSet.Add(current);
             else
             {
                 //breakpoint
             }
             List<Node> tempList = new List<Node>();// = neighborNodes[current];
 
             foreach(Node finderNode in neighborNodes.Keys)
             {
                 if (finderNode.Equals(current))
                 {
                     tempList = neighborNodes[finderNode];
                 }
             }
             if (current.pos.x == 0 && current.pos.y == 0)
             {
                 it = 10000;
                 //breakpoint
             }
             else
             {
                 //breakpoint
             }
             foreach(Node neighbor in tempList)
             {
                 if (Holds(closedSet,neighbor))// closedSet.Contains(neighbor))
                 {
                     continue;
                 }
                 //float ngScore = gScore[current] + (Vector3.Distance(current.pos,neighbor.pos)/4);//distBetween(current,neighbor);
                 float ngScore = Holds(gScore,current) + (Vector3.Distance(current.pos,neighbor.pos)/4);//distBetween(current,neighbor);
                 
                 if (!Holds(openSet,neighbor) || ngScore < gScore[neighbor])
                 {
                     cameFrom[neighbor] = current;
                     gScore[neighbor] = ngScore;
                     fScore[neighbor] = gScore[neighbor] + heurCost(neighbor,goal);
                     //if (!openSet.Contains(neighbor))
                     if (!Holds(openSet,neighbor))
                         openSet.Add(neighbor);
                 }
             }
         }
         return null;
     }
 


Can anyone help me make this actually get the proper path please?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

A* pathfinding - incorrect nodes calculated 1 Answer

A* Algorithm Node Grid Generation 2 Answers

A* pathfinding generating new path when created new obstacle 0 Answers

Custom pathfinding and node-values 1 Answer

Get current node in itween path 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges