Saturday, February 21, 2015

Finding the Path

Pathfinding! It's what makes units go around obstacles instead of through them, and it's something that is now in the game.

I used something called the A* algorithm to implement pathfinding. It's a bit complicated, but basically it calculates the shortest walkable route between two points. The algorithm itself is generic, of course, so I had to figure out how to implement it specifically in Unity. Unity does include a built-in navigation utility, but word on The Internet is that it doesn't always work very well. Plus building it myself gave me a much better understanding of how it works and how I can customize it to my needs.


The first step was to divide my map into a grid of walkable and unwalkable nodes:


The red squares represent unwalkable nodes and correspond to the position of obstacles in the scene:


When a unit receives a move order, it requests a path from the pathfinding script, which returns to it a list of waypoint nodes for the unit to follow:



Then the unit simply moves toward each waypoint until it has reached the destination node. It's a basic system, but it works.

There's plenty to be added to the pathfinding, of course - units will need to know how to move as a group, how to avoid running into each other, and how to avoid unfavorable terrain. Luckily the A* algorithm makes adding this type of functionality very easy.

What's the step? Well, in no particular order I want to add attack abilities, a pause menu and a main menu, and start integrating actual character models. Stay tuned!

Here's a free game about jet-packing around a cyber-punk city. It's a good example of a game with only one mechanic that works because that mechanic feels so good.

2 comments:

  1. Great progress and interesting information! Thanks for posting.

    ReplyDelete
  2. Always wondered how this was done. Clearly, as you demonstrate here, The art is in the execution. Very very cool!

    ReplyDelete