- Home /
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)
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)
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!
Your answer
Follow this Question
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