Below you can see my pathfinding pretty much working the way I need. I’m using AStar. There is a small issue with the stairs that I can’t seem to think of a solution. As you can see it the path takes a small shortcut and does not go up the stairs correctly.
Below is my logic for adding neighbors to path find and getting a Cost. Sadly my pathfinding is done in a separate thread so I cant use Unity Raycasting.
My main way of pathfinding to proper nodes is doing a height check between nodes and that’s how I implemented my stairs. Any help would be most appreciated.
public static void Expand(CPath path, CGridNode neighbour)
{
//Is neigbour null(Its out of bounds) && Is Walkable Check
if (neighbour == null || path.closed[neighbour.x, neighbour.y, neighbour.z] || !neighbour.isWalkable)
return;
//Height Check
if (neighbour.grid.GetHeightDiffrence(neighbour, path.best.current) > path.maxHeightToExpand)
return;
//Force 4way if we dont allow diganal movement
if (!path.allowDiagnal && Vector2.Distance(new Vector2(neighbour.x,neighbour.z),new Vector2(path.best.current.x, path.best.current.z)) > 1)
return;
//EXPAND
path.closed[neighbour.x, neighbour.y, neighbour.z] = true;
path.open.Add(new CExpansion(neighbour, path.best, CalculateCost(path, neighbour)));
}
public static float CalculateCost(CPath path, CGridNode gridNode)
{
float g = gridNode.grid.GetDistance(gridNode, path.startNode);
float h = gridNode.grid.GetDistance(gridNode,path.startNode);
float penalty = 0;
//Add penalty if Grid nodes is not on same layer to give better pathing
if (gridNode.y != path.best.current.y)
penalty += 100 * gridNode.y;
return g + h + penalty;
}
Here is a link to full Unity project on GitHub
https://github.com/nickkorta/CSharp-Unity-Pathfinding

