How to get distance between two hexagonal tile

plz check the picture, is there any method existing to calculate the distance? (just the distance, do not care if the cell is walkable or unwalkable)
I need something like this:

int GetDistance(GridNode node1, GridNode node2)

Hi

You can get a node’s coordinates using

x = node.XCoordinateInGrid;
z = node.ZCoordinateInGrid;

then you can use an off-the-shelf hexagon distance metric:

distance = max(
     abs(dest.y - start.y),     
     abs(dest.x - start.x),
     abs((start.x - start.y) - (dest.x - dest.y))
)

(I have not verified that this particular formula works, but if not that, at least a very similar formula for hex distances)

1 Like

Yes, it works, and now I want a similar function but the non-walkable should be taken into account for the GetDistance(GridNode node1, GridNode node2)

That sounds like you just want to calculate a path between A and B and check how many nodes it contains.

that’s it, where can I find the example code

You can find documentation about that here: Searching for paths - A* Pathfinding Project