[Solved] Dynamic Pathfinding Grid Issue
I have a cube that is set to a Layer that is called as "unwalkable" terrain. The cube interacts with the grid however it appears to be offset and whenever the cube is moved to the top of the grid, the "unwalkable" tiles appear on the bottom of the grid.
I am using the Tutorials from CodeMonkey and Sebastian Lague on Youtube. I am attempting to combine both of their works to create a dynamic pathfinding for the project I am working on. I have combed through all the code and change everything I could think of in an attempt to fix this but I can't get anything to work.
I would appreciate any help I can get.
This is the code I am using:
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Grid : MonoBehaviour
{
public bool displayGridGizmos;
public LayerMask unwalkableMask;
private GridNode[,] gridArray;
public int columns, rows;
public float cellSize;
public Vector3 gridBottomLeft;
void Awake(){
CreateGrid();
}
void Update(){
SetIsWalkable();
}
void CreateGrid(){
gridArray = new GridNode[columns, rows];
for (int x = 0; x < gridArray.GetLength(0); x++){
for (int y = 0; y < gridArray.GetLength(1); y++){
Vector3 nodePos = GetWorldPosition(x, y);
bool walkable = !(Physics.CheckSphere(nodePos, cellSize / 2, unwalkableMask));
gridArray[x, y] = new GridNode(x, y, nodePos, walkable, 0f, 0f);
}
}
}
public Vector3 GetWorldPosition(int x, int y){
gridBottomLeft = transform.position - Vector3.right * columns / 2 * cellSize - Vector3.forward * rows / 2 * cellSize;
Vector3 worldPoint = gridBottomLeft + Vector3.right * (x * cellSize + cellSize / 2) + Vector3.forward * (y * cellSize + cellSize / 2);
return worldPoint;
}
public void GetXY(Vector3 wPosition, out int x, out int y) {
x = Mathf.FloorToInt((wPosition - gridBottomLeft).x / cellSize);
y = Mathf.FloorToInt((wPosition - gridBottomLeft).z / cellSize);
}
public GridNode GetGridObject(int x, int y) {
if (x >= 0 && y >= 0 && x < columns && y < rows) {
return gridArray[x, y];
} else {
return default;
}
}
public GridNode GetGridObject(Vector3 worldPosition) {
int x, y;
GetXY(worldPosition, out x, out y);
return GetGridObject(x, y);
}
public void SetIsWalkable(){
foreach (GridNode n in gridArray){
gridArray[n.x, n.y].isWalkable = !(Physics.CheckSphere(GetWorldPosition(n.x, n.y), cellSize / 2, unwalkableMask));
}
}
void OnDrawGizmos(){
Gizmos.DrawWireCube(transform.position, new Vector3(columns, 1, rows) * cellSize);
if (gridArray != null && displayGridGizmos){
foreach (GridNode n in gridArray){
Gizmos.DrawCube(n.nodeWorldPosition, Vector3.one * (cellSize - .1f));
Gizmos.color = (n.isWalkable) ? Color.white : Color.red;
}
}
}
}
Answer by unity_7jx7YddzbCkq8A · Aug 16, 2020 at 02:20 AM
I finally got it working. I had to take some time away from the project to clear my mind. I realized that I was trying to pull in locations and values from another class and that are not available when the Gizmos are created. I moved the position code into the class that was creating the gizmos, and now everything works swimmingly.
Also, my apologies, I realize now that when I posted this question I didn't include the other class scripts I am using.
Your answer
![](https://koobas.hobune.stream/wayback/20220613005424im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity AI problem 1 Answer
Connecting Pods/Nodes together on a grid using pathfinding? 0 Answers
Hexagonal Grid Rangefinding 1 Answer
A* Pathfinding and keep the enmy at range 0 Answers
InvalidOperationException thrown 0 Answers