Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Sici9 · Apr 12, 2019 at 10:47 PM · coroutinepathfindingastarupdating

A* pathfinding: neighbor list not updating

Hey everybody, I need your help! I am trying to implement A* pathfinding for a 2D game. I am almost finished: I manage to create a node-based grid, distinguish walkable and unwalkable tiles, I can compute the G, H and Fcost and I created a method that, given the list of nodes surrounding the current one, can determine the one with the lowest cost. It is with last method however, that I think there is some kind of problem: basically as I run the method, it successfully manages to find its neighoring nodes and the best one among them (see first picture) alt text

The yellow nodes are the neighbors, whereas the green is the best node so far. After this first step, the idea is to update the neighbor list with respect to the new node (see second picture)

alt text

So far so good! The problem arise now: looking at the results I am getting, it seems that after this iteration, the best node will not be updated, and consequentially neither the neighbors will be! I have been trying several things but I cannot find any solution, I may need the help of somebody with a fresh perspective (and better programming skills than mine :D).

The script in which I retrieve the bestnode and theoretically update the neighbors is this (attached to the player):

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

public class BestNode : MonoBehaviour {

 public Tilemap walkableTM;
 public GameObject candidate;
 public GameObject target;
 
 public IEnumerator ReachTarget() {

     GameObject newCandidate = candidate;
     Debug.Log("IE new candidate: " + newCandidate);
     yield return new WaitForSecondsRealtime(1f);
     candidate = NewBestNode(target, newCandidate );
     Debug.Log("IE updated candidate: " + candidate );
     if (candidate == target)
     {
         Debug.Log("IE You reached the target!");
         StopAllCoroutines();
     }
     else {
         Debug.Log("IE Retry!");
         
         StartCoroutine(ReachTarget());
     }
 }

 private void Start()
 {

 }
 void Update()
 {
     
     
     if (Input.GetKeyDown(KeyCode.Mouse1)) {
         candidate = GetComponent<Player_Move>().FindClosestNode();
         Debug.Log("INP first candidate:" + candidate);
         target = GetComponent<Player_Move>().FindClosestTargetNode();
         Debug.Log("INP target:" + target);
         StartCoroutine(ReachTarget());
     }
 }
 public GameObject NewBestNode(GameObject target, GameObject start) {
     int currentBestF = 1000;

     GameObject bestCandidate = new GameObject();
     Vector3Int startPosition = new Vector3Int(Mathf.RoundToInt(start.transform.position.x), Mathf.RoundToInt(start.transform.position.y), 0);
     Vector3Int targetPosition = new Vector3Int(Mathf.RoundToInt(target.transform.position.x), Mathf.RoundToInt(target.transform.position.y), 0);
     walkableTM.GetComponent<TileManager>().GetNeighbors(candidate);

     foreach (var item in walkableTM.GetComponent<TileManager>().neighboringTiles)
     {
         if (walkableTM.GetComponent<TileManager>().closedList.Contains(item)) { }//I need this to be sure that i am not using unallowed nodes
         else
         {
             //Looks for node in list with lowest cost
             Vector3Int candidatePosition = new Vector3Int(Mathf.RoundToInt(item.transform.position.x), Mathf.RoundToInt(item.transform.position.y), Mathf.RoundToInt(item.transform.position.z));

             int currentGCost = item.GetComponent<Node>().GetGCost(startPosition, candidatePosition);
             int currentHCost = item.GetComponent<Node>().GetHCost(candidatePosition, targetPosition);
             int currentFcost = currentGCost + currentHCost;
             if (currentFcost < currentBestF)
             {
                 currentBestF = currentFcost;

                 bestCandidate = item.gameObject;
             }
         }
     }
     bestCandidate.GetComponent<SpriteRenderer>().color = Color.green;//For debugging reasons
     Debug.Log("Best node in: " + bestCandidate.transform.position + ", with Fcost: " + currentBestF);
     return bestCandidate;
 }
 


}

And additionally, this is the method that looks for the neighbors, it is inside a larger script attached to the ground tilemap but I will limit to this as I do not want to make the question too large:

public List GetNeighbors(GameObject node) {

     Vector2Int nodePosition = new Vector2Int(Mathf.RoundToInt(node.transform.position.x), Mathf.RoundToInt(node.transform.position.y));
     var allNeighbors = Physics2D.OverlapCircleAll(nodePosition, 1.2f);
     Debug.Log("GN looking for neighbors" +  node.name);
     foreach (var item in allNeighbors)           
     {
         if (item.CompareTag("NodeUI"))
         {
             if (neighboringTiles.Contains(item.gameObject)) { }
                 else { 
                 neighboringTiles.Add(item.gameObject);
                 item.GetComponent<SpriteRenderer>().color = Color.yellow;
                 }   
         }
         else { }
     }
         return neighboringTiles;
 }


There are 2 other functions used inside the first script, one that snaps to the node closest to the player and the second that does the same for the current target, I don't think that the problem is there so it is not worth showing them, but if somebody actually realise that the problem is precisely there I can add them! Thanks to anybody who could give me even an hint!

test1.png (423.2 kB)
test2.png (416.7 kB)
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.

Follow this Question

Answers Answers and Comments

113 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

Related Questions

How do i get the IsPathPossible() function to ignore some nodes using Astar Pathfinding Project 0 Answers

Implementation of Navmesh terrain dijkstra algorithm 0 Answers

Aron Ganberg Astar - add nodes at runtime with pointgraph 0 Answers

How to rotate object to face toward the path using Astar pathfinding? 0 Answers

A* Pathfinding on click? 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