Pathfinding is functioning correctly, and the colors are changing as expected. However, I am noticing an issue where more yellow tiles appear on the left side, and I don’t understand why.
Below is the path finder script
‘’'using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathFinder : MonoBehaviour
{
[SerializeField] Vector2Int startCoordinates;
[SerializeField] Vector2Int destinationCoordinates;
Node startNode;
Node destinationNode;
Node currentSearchNode;
//nodes that aren't explored
Queue<Node> frontier = new Queue<Node>();
Vector2Int[] directions = {Vector2Int.right, Vector2Int.left, Vector2Int.up, Vector2Int.down};
GridManager gridManager;
Dictionary<Vector2Int, Node> grid = new Dictionary<Vector2Int, Node> ();
//as in our tree we will include each node only once thats why keeping track
Dictionary<Vector2Int, Node> reached = new Dictionary<Vector2Int, Node>();
private void Awake()
{
gridManager = FindObjectOfType<GridManager>();
if (gridManager != null )
{
grid = gridManager.Grid;
}
}
void Start()
{
startNode = gridManager.Grid[startCoordinates];
destinationNode = gridManager.Grid[destinationCoordinates];
BreadthFirstSearch();
BuildPath();
}
void ExploreNeighbors()
{
List<Node> neighbors = new List<Node>();
foreach(Vector2Int direction in directions)
{
Vector2Int neighborcoords = currentSearchNode.coordinates + direction;
if (grid.ContainsKey(neighborcoords))
{
neighbors.Add(grid[neighborcoords]);
//Debug.Log(grid[neighborcoords].coordinates);
/*TODO: Remove after testing
grid[neighborcoords].isExplored = true;
grid[currentSearchNode.coordinates].isPath = true;*/
}
}
foreach(Node neighbor in neighbors)
{
if (!reached.ContainsKey(neighbor.coordinates) && neighbor.isWalkable)
{
neighbor.connectedTo = currentSearchNode;
reached.Add(neighbor.coordinates, neighbor);
frontier.Enqueue(neighbor);
}
}
}
void BreadthFirstSearch()
{
bool isRunning = true;
frontier.Enqueue(startNode);
reached.Add(startCoordinates, startNode);
while (frontier.Count > 0 && isRunning)
{
currentSearchNode = frontier.Dequeue();
currentSearchNode.isExplored = true;
ExploreNeighbors();
if (currentSearchNode == destinationNode)
{
isRunning = false;
}
}
}
List<Node> BuildPath()
{
List <Node> path = new List<Node>();
Node currentNode = destinationNode;
path.Add(currentNode);
currentNode.isPath = true;
while (currentNode.connectedTo != null)
{
currentNode = currentNode.connectedTo;
path.Add(currentNode);
currentNode.isPath = true;
}
path.Reverse();
return path;
}
}
‘’’
CoordinateLabeler Script:
‘’'using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
//atatching this script to text as it need to be changed
[ExecuteAlways] //as we want to see changes in edit mode too
//it is a flag
[RequireComponent(typeof(TextMeshPro))]
public class CoordinatesLabeler : MonoBehaviour
{
[SerializeField] Color defautColor = Color.white;
[SerializeField] Color blockedColor = Color.gray;
[SerializeField] Color exploredColor = Color.yellow;
[SerializeField] Color pathColor = new Color(1f, 0.5f, 0f);
TextMeshPro Label;
Vector2Int coordinates = new Vector2Int();
GridManager gridManager;
void Awake()
{
Label = GetComponent<TextMeshPro>();
gridManager = FindObjectOfType<GridManager>();
DisplayCoordinates();
Label.enabled = false;
}
void Update()
{
//will just execute in edit mode
if (!Application.isPlaying)
{
DisplayCoordinates();
UpdateObjectName();
Label.enabled = true;
}
SetLabelColor();
ToggleLabels();
}
private void ToggleLabels()
{
if (Input.GetKeyDown(KeyCode.C))
{
//IsActive gives current active state
Label.enabled = !Label.IsActive();
}
}
private void SetLabelColor()
{
if (gridManager == null) { return; }
Node node = gridManager.GetNode(coordinates);
if (node == null) { return; }
if (!node.isWalkable)
{
Label.color = blockedColor;
}
else if(node.isPath) //did path before explored as it comes under explored if explored first we will never reach to path
{
Label.color = pathColor;
}
else if(node.isExplored)
{
Label.color = exploredColor;
}
else
{
Label.color = defautColor;
}
}
private void DisplayCoordinates()
{
if (gridManager == null) { return; }
//transform.parent as text is a child obj
coordinates.x = Mathf.RoundToInt(transform.parent.position.x / gridManager.UnityGridSize);
coordinates.y = Mathf.RoundToInt(transform.parent.position.z / gridManager.UnityGridSize);
Label.text = coordinates.x + "," + coordinates.y;
}
private void UpdateObjectName()
{
transform.parent.name = coordinates.ToString();
}
}
‘’’
I am also having confusion regarding what exactly isExplored refers to