Friday, June 17, 2011

Unity, hexagons and path-finding

I’m creating a strategy computer game again. This time I thought I’d write some tutorials as I go. In this tutorial, I bring path-finding on an hexagonal grid together with Unity.

I assume a good knowledge of C# and no knowledge of Unity. If you know how the A* search algorithm works, you’ll probably be able to read all the code without any problems.

You can access the tutorial here.
The source code and Unity project file are in a github repo.
A Windows binary is available here.
A Mac binary is available here.


Screenshot of the result

9 comments:

  1. To see how you can take path-finding and Unity further, have a look at Hexagonal grid: Path-finding using A* algorithm and the other tutorials there.

    ReplyDelete
  2. how can i use class "PathFind"? i need to set:
    1) hex grid(countX,countY)
    2) startPosition(X,Y)
    3) endPosition(X,Y)
    what i want to return:
    Path everyStep
    i make the game by Unity, please help me figure out

    ReplyDelete
  3. Hi I know this blog is old, but I was wondering if you could help: I found your source code via the links you provided and I am a C# developer with no unity experience - I am trying to import the files into a modern instance of unity (2020.3.25f1) but (surprise surprise) it won't have any of it and I was wondering if you could help get a build in a modern unity?

    ReplyDelete
    Replies
    1. Hi Conners,

      I saw your comment about a day after you posted. I thought "yeah, that sounds like a reasonable request. How hard can it be?" Except that was 10 years ago! I've half forgotten how Unity works.

      Anyways, after a proper effort today, I have it working again.

      https://github.com/ledpup/HexagonalPathfinding

      Delete
  4. You are a legend thank you! Happy New Years mate!

    ReplyDelete
  5. I am rewriting a game from 23 years ago called warfleet, (I still own fleetwar.com and warfleet.com.from back in the day) but that was all in browser and grid. I wanted this new one to be 3D in hexagons. Always wanted to do this. Great help mate. I'll give u a credit and link you when I'm done.

    ReplyDelete
    Replies
    1. Hey no problem. I extended the pathfinding functions for my own game (that I never finished... yet). It has tile costs in it. It might be useful to you. https://github.com/ledpup/StrategyGame/tree/master/PathFind

      I.e., I had it working with flying units that could cross over water-bodies, island hopping as they go. That part works quite well I think. I just couldn't figure out a good way to do the AI.

      Delete
  6. AI is easy. You just have an integer intelligence score for a unit. Then you pick that many move + attack options. And score them. Then pick the best score. It sounds primitive but I did it for real and it was remarkably lifelike. You can also do combos with a similar method. You roll for each target rather than each attacker. (IE: ask the proposition if I had to kill this unit what would be the best probability score) then you have a bubble list of turn moves that removes crap scores in preference. It was very fast to run and very lifelike

    ReplyDelete