Pathfinding on a hexagonal grid


Pathfinding on a hexagonal 3D grid turned out to be easier than I first thought. It was next on my todo-list and I had been putting it off for a week in fear of what it might uncover. A previous attempt to implement pathfinding made me realize how my grid coordinate system was broken and lead me to rebuild it from the bottom up.

Using the excellent pseudo code from https://www.redblobgames.com/grids/hexagons/ turned out to be easy enough. Unity is currently lacking the C# priorityqueue, but with some googling I found a helpful post that ported it over to a class usable in Unity.

//Snippet

Once the pathfinding was in place I went about representing it in world space. A line renderer was used for displaying the path on the map and the line renderers individual point was set to the middle and the edge between each hexagon along the path. 

//Setting and unsetting the pathfindingLine and creating the dots on each hexagon

Creating the display of movement cost over the path is done via a shader. This shader took some time to build but it is simply put combining individual textures into a string of numbers corresponding to the value passed in. 


//Spaghetti


End result:


Leave a comment

Log in with itch.io to leave a comment.