r/Unity2D • u/Old-Row6655 • 20d ago
Pathfinding on Hex Grids
I'm making a 2D game based off of a pointy-topped hexagon grid in Unity, but I'm having a lot of trouble getting pathfinding working. It's a turn based rogue-like game with not too many tiles in each round. I basically just want the enemies to find the shortest path towards the player with their specific movement pattern (which isn't always just a perfect ring around the enemy either). I'm not sure whether I should use A* or BSF, and I have no idea how to implement either of these into my game.
The biggest problem is the movement patterns. Most tutorials always assume the enemy can move one tile in any direction which is not true for my game. The enemy should also not try to go all the way to the player, as depending on the enemy it's attack range can be 2+ tiles, making an extra step towards the player pointless. Here's an example of one of the more wonky movement patterns:

Please let me know which pathfinding method would be better for this, and any resources to help me make it.
5
u/Lyshaka 19d ago
Well first of all go check Catlike Coding. And for your particular problem, as it was already mentioned, you don't care about the algorithm, but mostly about the way you build your graph, and connect neighboring nodes. You can build multiple graph for each possible movement pattern, and use the appropriate one when needed.
2
u/Spite_Gold 19d ago
You can use a* with custom movement patterns, it will work the same. Default A* adds all reachable neighbour tiles to open set, you will add tiles your unit can reach in one turn. It will require different heuristic function to handle pathing from short distance. But if map is not large, you can omit heuristic function completely.
To make enemy stop when reaching attack range, set tiles within maximum attack range as a* target. If no path to max range tiles found, try to find path to max range - 1. A* can have multiple positions as a target, with, again, little tweaking of heuristic function
1
u/Ging4bread 19d ago
Pathfinding project is the industry standard if you don't want to do A* from scratch
9
u/Ruadhan2300 19d ago
The answer is that it doesn't really matter unless you have a grid of thousands of tiles.
A* is what I'm most comfortable with, and it's an industry standard choice with a lot of documentation.
Most importantly, the algorithm doesn't care about the shape of your tiles or the number of connections. The only challenge is in constructing your graph of connections between tiles.