• 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 question was closed Apr 15, 2021 at 09:30 PM by nightpredetor for the following reason:

Other

avatar image
0
Question by nightpredetor · Apr 15, 2021 at 07:16 AM · 2dbug

Cant add to List. Or maybe an infinite loop.

I am trying to make an A* Algorithm Pathfinder. The problem is that whenever I try to run the code in unity, the game freezes on play. The problem does not occur when I comment the #64 line i.e openSet.Add(neighbour); or when I comment the lines #27, #28, #68 which make the while loop condition.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class AStar : MonoBehaviour { public Object prefab;

 public Transform seeker; // the set of nodes to be evaluated
 public Transform target; // the set of nodes already evaluated

 void Update()
 {
         PathFinding(seeker.position, target.position);
 }

 void PathFinding(Vector2 startPos, Vector2 targetPos)
 {
     Node startNode = new Node(startPos);
     Node targetNode = new Node(targetPos);

     List<Node> openSet = new List<Node>();
     HashSet<Node> closedSet = new HashSet<Node>();

     openSet.Add(startNode);

     while(openSet.Count > 0) // assign currentNode as the node with the lowest fCost in the open set
     {
         Node currentNode = openSet[0];

         for(int i = 1; i < openSet.Count; i++)
         {
             if(openSet[i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost)
             {
                 currentNode = openSet[i];
             }
         }

         openSet.Remove(currentNode); // remove currentNode from open set
         closedSet.Add(currentNode); // add currentNode to closed set

         if(currentNode == targetNode) // if currentNode is equal to targetNode than path has been found
         {
             RetracePath(startNode, targetNode);
             return;
         }

         foreach(Node neighbour in GetNeighbours(currentNode))
         {
             if(!neighbour.walkable || closedSet.Contains(neighbour))
             {
                 continue;
             }

             float newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour);
             if(newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
             {
                 neighbour.gCost = newMovementCostToNeighbour;
                 neighbour.hCost = GetDistance(neighbour, targetNode);
                 neighbour.parent = currentNode;

                 if(!openSet.Contains(neighbour))
                 {
                     openSet.Add(neighbour); // !!!PROBLEM!!!
                 }
             }
         }
     }
 }

 List<Node> GetNeighbours(Node node)
 {
     List<Node> neighbours = new List<Node>();

     for(int x = -1; x <= 1; x++)
     {
         for(int y = -1; y <= 1; y++)
         {
             if(x == 0 && y == 0)
             {
                 continue;
             }

             float checkX = node.position.x - x;
             float checkY = node.position.y - y;
             bool checkWalkable = !(Physics2D.OverlapCircle(new Vector2(checkX, checkY), 0.5f, LayerMask.GetMask("Obstacle")));

             neighbours.Add(new Node(checkWalkable, new Vector2(checkX, checkY)));
         }
     }

     return neighbours;
 }

 float GetDistance(Node nodeA, Node nodeB)
 {
     float disX = Mathf.Abs(nodeA.position.x - nodeB.position.x);
     float disY = Mathf.Abs(nodeA.position.y - nodeB.position.y);

     if(disX > disY)
     {
         return 14 * disY + 10 * (disX - disY);
     } else
     {
         return 14 * disX + 10 * (disY - disX);
     }
 }

 void RetracePath(Node startNode, Node endNode)
 {
     List<Node> path = new List<Node>();
     Node currentNode = endNode;

     while(currentNode != startNode)
     {
         path.Add(currentNode);
         currentNode = currentNode.parent;
     }

     path.Reverse();

     foreach(Node step in path)
     {
         GameObject stepPrefab = (GameObject)Instantiate(prefab, new Vector3(step.position.x, step.position.y, 0f), Quaternion.identity);
         stepPrefab.GetComponent<SpriteRenderer>().color = Color.green;
     }
 }

}


using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Node { public bool walkable; public Vector2 position;

 public Node parent;

 public float gCost, hCost;

 public float fCost
 {
     get
     {
         return gCost + hCost;
     }
 }

 public Node(Vector2 pos)
 {
     position = pos;
 }

 public Node(bool _walkable, Vector2 pos)
 {
     walkable = _walkable;
     position = pos;
 }

}

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

  • Sort: 

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

287 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Line of code skips randomly at times? 1 Answer

Does anyone know how to fix this tilemap render issue? 0 Answers

Object Changing Y Coordinate in Game View 0 Answers

Weird blue lines in scene and game view. 0 Answers

Sprite changing position during rotation...bug? 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