I am trying to write a simple bot that will find a path on the map from point A to point B and navigate along the established shortest path. I try to write the whole algorithm with my own, I know that there are such things as NavMeshSurface + NavMeshAgent, but I need to implement everything with my own.
For the implementation, I chose the A star pathfinding algorithm and implemented it. There was a problem with defining walls in the path, I solved this problem by creating a 2D mesh and checking the WallLayout. As a result, for now, the bot finds the shortest path to the point and moves there.
The problem is uneven surfaces. The whole algorithm and meshing works on a flat surface, if I add a surface with some kind of slope, for example, any upward rise, then I am not catching up on how to correctly plot the coordinates of the path.
Mesh script:
public class Grid : MonoBehaviour
{
[SerializeField]
private Transform StartPosition;
[SerializeField]
private LayerMask WallMask;
[SerializeField]
private Vector2 vGridWorldSize;
[SerializeField]
private float fNodeRadius;
[SerializeField]
private float fDistanceBetweenNodes;
Node[,] NodeArray;
public List<Node> FinalPath;
float fNodeDiameter;
int iGridSizeX, iGridSizeY;
private void Start()
{
fNodeDiameter = fNodeRadius * 2;
iGridSizeX = Mathf.RoundToInt(vGridWorldSize.x / fNodeDiameter);
iGridSizeY = Mathf.RoundToInt(vGridWorldSize.y / fNodeDiameter);
CreateGrid();
}
void CreateGrid()
{
NodeArray = new Node[iGridSizeX, iGridSizeY];
Vector3 bottomLeft = transform.position - Vector3.right * vGridWorldSize.x / 2 - Vector3.forward * vGridWorldSize.y / 2;
for (int x = 0; x < iGridSizeX; x++)
{
for (int y = 0; y < iGridSizeY; y++)
{
Vector3 worldPoint = bottomLeft + Vector3.right * (x * fNodeDiameter + fNodeRadius) + Vector3.forward * (y * fNodeDiameter + fNodeRadius);
bool Wall = true;
if (Physics.CheckSphere(worldPoint, fNodeRadius, WallMask))
{
Wall = false;
}
NodeArray[x, y] = new Node(Wall, worldPoint, x, y);
}
}
}