Hi,
I just implemented the part where the coordinate labeler script is updated to use the status of the nodes isWalkable, isExplored and isPath flags to decide the colour of the coordinate label.
The labels for the blocked and walkable tiles are of the correct colours in the editor mode, but when I run the code in game mode to calculate paths using the pathfinder, the label colouring breaks. All the tile are shown as blocked. The path calculated or the nodes explored by the Pathfinder are not shown in the orange and yellow colours respectively. The label colours for the path and explored nodes was previously implemented and working perfectly but broke when the decision making for the colour was switched from isPlaceable flag to node.isWalkable flag.
After extensive debugging, I have figured that the Tile script (where the BlockNode) function is called, is not the issue, as it is only called for tiles where the isPlaceable flag is false. The value of isWalkable for the tiles is correct, irrespective of whether the tile is blocked or not. Hence, I deduced that the problem must be in the Coordinate Labeler script only, although I may be completely wrong.
I am attaching the following below
- my CoordinateLabeler.cs
- my Tile.cs (formerly Waypoint.cs)
- my GridManager.cs
- Screenshots of Editor Mode and Game Mode
I have run out of ideas to try and I am getting pretty frustrated with this problem. Any kind of help is really appreciated.
CoordinateLabeler.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
[ExecuteAlways] [RequireComponent(typeof(TMP_Text))]
public class CoordinateLabeler : MonoBehaviour
{
[SerializeField] Color defaultColor = Color.white;
[SerializeField] Color blockedColor = Color.gray;
[SerializeField] Color traversedColor = Color.yellow;
[SerializeField] Color pathColor = new Color(1f, 0.5f, 0);
TextMeshPro label;
Vector2Int coordinates = new Vector2Int();
GridManager gridManager;
Dictionary<Vector2Int, Node> grid;
void Awake() {
label = GetComponent<TextMeshPro>();
label.enabled = false;
DisplayCoordinates();
gridManager = FindObjectOfType<GridManager>();
}
// Update is called once per frame
void Update()
{
if (!Application.isPlaying) {
// run code in scene view only
DisplayCoordinates();
UpdateObjName();
label.enabled = true;
}
SetLabelColor();
ToggleLabels();
}
void ToggleLabels() {
if (Input.GetKeyDown(KeyCode.C)) {
label.enabled = !label.enabled;
}
}
// Sets the label colour
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) {
// Debug.Log("Path color for " + coordinates);
label.color = pathColor;
} else if (node.isTraversed) {
// Debug.Log("Traversed color for " + coordinates);
label.color = traversedColor;
} else {
// Debug.Log("Normal color for " + coordinates);
label.color = defaultColor;
}
// }
}
void DisplayCoordinates() {
if (gridManager == null) { return; }
coordinates.x = Mathf.RoundToInt(transform.parent.position.x / gridManager.UnityGridSize);
coordinates.y = Mathf.RoundToInt(transform.parent.position.z / gridManager.UnityGridSize);
label.text = coordinates.x.ToString() + ',' + coordinates.y.ToString();
}
void UpdateObjName() {
transform.parent.name = coordinates.ToString();
}
}
Tile.cs (formerly Waypoint.cs)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tile : MonoBehaviour
{
[SerializeField] bool isPlacable;
public bool IsPlacable { get { return isPlacable; }}
[SerializeField] GameObject ballista;
GridManager gridManager;
Vector2Int coordinates;
void Awake() {
gridManager = FindObjectOfType<GridManager>();
}
void Start() {
if (gridManager != null) {
coordinates = gridManager.GetCoordinatesFromPosition(transform.position);
if (!isPlacable) {
gridManager.BlockNode(coordinates);
}
}
}
private void OnMouseDown() {
if (isPlacable) {
bool isPlaced = ballista.GetComponent<Tower>().createTower(ballista, transform.position);
isPlacable = !isPlaced;
}
}
}
GridManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridManager : MonoBehaviour
{
Dictionary<Vector2Int, Node> grid = new Dictionary<Vector2Int, Node>();
public Dictionary<Vector2Int, Node> Grid { get { return grid; } }
[SerializeField] Vector2Int gridSize;
[SerializeField] int unityGridSize = 10;
public int UnityGridSize { get { return unityGridSize; }}
void Awake() {
CreateGrid();
}
public Node GetNode(Vector2Int coordinates) {
if (grid.ContainsKey(coordinates)) {
return grid[coordinates];
}
return null;
}
public void BlockNode (Vector2Int coordinates) {
if (grid.ContainsKey(coordinates)) {
grid[coordinates].isWalkable = false;
// Debug.Log("Blocking node for " + coordinates + grid[coordinates].isWalkable);
}
}
public Vector2Int GetCoordinatesFromPosition (Vector3 position) {
Vector2Int coordinates = new Vector2Int();
coordinates.x = Mathf.RoundToInt(position.x / unityGridSize);
coordinates.y = Mathf.RoundToInt(position.z / unityGridSize);
return coordinates;
}
public Vector3 GetPositionFromCoordinates (Vector2Int coordinates) {
Vector3 position = new Vector3();
position.x = coordinates.x * unityGridSize;
position.y = 0;
position.z = coordinates.y * unityGridSize;
return position;
}
void CreateGrid() {
for (int x = 0; x < gridSize.x; x++)
{
for (int y = 0; y < gridSize.y; y++)
{
Vector2Int coordinates = new Vector2Int(x, y);
grid.Add(coordinates, new Node(coordinates, true));
}
}
}
}
Screenshot of Editor Mode
Screenshots of Game Mode