By Published On: May 30th, 2023Categories: General1 CommentTags: , , , , ,

I’ve been using this gist in my last two projects to get an easy hex grid up and running quickly in Unity. I made some additions to it using the original source material at redblobgames.com, including an implementation of Amit’s A* Pathfinding algorithm (definitely one of my prouder comp-sci/math moments, even though I already did my own variation on A* back in Tower2Defense).

Here’s a video of the A* pathfinding in action (the enemies take damage randomly as they walk, no player towers yet in this video):

And some fun experiments with enumerating through the grid using Spiral, Ring & DistanceTo to create the game board’s pattern (the gif with enemies is actually using Unity’s AI Navigation package, which was not worth continuing to struggle with compared to the results the A* gives me now):

Finally, here’s the updated Hex.cs so you can check it out:
(Unity doesn’t include C#’s PriorityQueue class yet; you can use this direct port from the official C# lib as a drop-in replacement: https://github.com/FyiurAmron/PriorityQueue/blob/main/PriorityQueue.cs)

View this gist on GitHub

Programmer code on a screenLightweight ScriptableObject Events in Unity
Computer coding on a screenUpdated Lightweight ScriptableObject Events in Unity

One Comment

  1. […] Pathfinding for the enemies to find their way to the grid center (check out my Hex Grid Framework here). A major part of my thesis was a Tower Defense game where the enemy spawn point changed throughout […]

Leave a Reply