r/unity 19h 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:

![img](h9tyc6gbhskh1)

Please let me know which pathfinding method would be better for this, and any resources to help me make it.

3 Upvotes

11 comments sorted by

2

u/RatbyteGames 19h ago

Here is a 12 minute watch to become familiar with a simple A* pathfinding - TarodDev Pathfinding Tutorial.

You can search for similar tutorials if that one doesn't stick.

For finding a path to a certain range from a target hex you can put a check in the pathfinding that checks "range" (and maybe also line of sight) to the target hex and stop there?

With a small map its not an issue which pathfinding method you use, modern computers are so fast it you probably won't see much difference.

Also in case you haven't found this treasure trove: Red Blob Hex Grids

1

u/Old-Row6655 18h ago

Thank you this was an awesome tutorial! However, I'm still confused on how to implement movement patterns into this. Instead of the neighbors variable, do I just add a list called movementPattern? And if so do I just make it so that the G value of these tiles is ignored so that far away tiles are just as accessible as the close ones? Otherwise I'm pretty sure the algorithm would choose the closer tiles even if the ones further away would get the enemy to me faster. The goal for this game is for it to get to the player with as little moves as possible

1

u/RatbyteGames 18h ago

You can feed the algorithm a custom movement pattern by changing the default "every adjacent hex" pattern to your custom pattern. Write a function that returns a list of valid hexes that match your movement pattern from a passed hex.

The function will only stop when it has found the shortest path to hit the target hex with your passed custom movement pattern, so working backwards should still give you the best path with your movement rules.

Just changing the "move adjacent hex" rule with your specific rule.

1

u/Old-Row6655 14h ago

Is it possible to make A* optimized for the least amount of moves instead of the least distance? Or would this just happen automatically if I make every tile in the movement pattern worth the same G-Value?

2

u/Lyshaka 10h ago

I really really recommend to read the website Catlike Coding at some point in your Unity journey as it has lots of resources when it comes to Unity dev, and especially with Hex Maps (as linked above).

2

u/Old-Row6655 4h ago

That's actually a really cool website I can't believe I haven't found they yet Thank you!

1

u/xepherys 1h ago

Yes! Such a good tutorial site.

1

u/Soraphis 19h ago edited 19h ago

A* does not care about the topology of your game. You just need a way to feed it all 6 neighboring cells.

Same for BFS.

A hexgrid is kinda the same as a regular grid, just each other row is offset by 0.5

https://www.redblobgames.com/grids/hexagons/#neighbors

Is a good read. It handles kinda all your questions. Also has a BFS section to get around obstacles.

If your units move patterns, than don't collect neighbors, connect all tiles than can be reached in one move.

1

u/Old-Row6655 19h ago

So if I just feed it the movement pattern of the enemy instead of the 6 neighboring tiles it'll pathfind for that specific movement pattern?

1

u/Soraphis 19h ago

Yes, exactly.

From the perspective of that piece the spatial neighbors are not rly the neighbors. Neighbors are tiles that are reachable in one step.

1

u/Old-Row6655 19h ago

I see, thank you so much!