A simple path-finding example

This example shows how to use the path-finding algorithm that come with Grids on a grid built in the editor (as opposed to a grid built in code).

You can also check out the Path-Finding Tutorial, and a path-finding example with layered grids.

To run the example:

  1. Import Grids in a clean project.
  2. Import one of the packages below into the project.
  3. Open the PathFinding scene, and run it.

Use left-click to toggle whether cells are “walkable”. Right click to alternately place the start and goal. In the Rect example, play around with different neighbor setups to see ho it affects the path.

PathFinding_Hex.unitypackage

PathFinding_Rect.unitypackage

pathfinding_hex

As is often the case when we build examples like this, we discovered two minor flaws in our design.

The first has to do with the CloneStructure method. This method is extremely useful – it allows you to clone a grid in terms of its shape and size, but it can take values of a different type. The idea is to make it easy to make a new grid that is exactly the same as the old grid, except for the contents. It is also used in the CastValues method, which returns a new grid in the same shape as the original, but with the contents casted to a new given type.

Unfortunately, at this moment, CloneStructure (and hence CastValues) does not preserve neighbor relations. So if you set the neighbors of a rect grid to be along diagonals, the clone will not have the same setup – it will still have the default setup. Currently, this affects only rect grids and diamond grids (as they are the only grids whose neighbors can be configured).

The workaround is simply to reconfigure neighbors on the clone.

And trying this workaround while making the example, this is exactly how we discovered the second minor flaw. The GridBuilder does not expose the neighbor setup publicly!

So in  this example, we just run the path finding on the original grid; you can get more details on this in the comments in the code.

We will of course address both these issues in an update!

Scroll to Top