项目作者: RonenNess

项目描述 :
A very simple 2d tile-based pathfinding for unity, with penalty supported
高级语言: C#
项目地址: git://github.com/RonenNess/Unity-2d-pathfinding.git
创建时间: 2016-05-06T00:53:08Z
项目社区:https://github.com/RonenNess/Unity-2d-pathfinding

开源协议:MIT License

下载


Unity-2d-pathfinding

A very simple 2d tile-based pathfinding for unity, with tiles price supported.

NEW REPO

I moved this script to a dedicated repo for Unity utilities.

A newer version can be found here along with other useful Unity scripts.

This repo will remain online but won’t be maintained.

About

This code is mostly based on the code from this tutorial, with the following modifications:

  • Removed all rendering and debug components.
  • Converted it into a script-only solution, that relay on grid input via code.
  • Separated into files, some docs.
  • A more simple straight-forward API.
  • Added support in tiles price, eg tiles that cost more to walk on.

But overall most of the credit belongs to Sebastian Lague, so show him your love.

How to use

First, copy the folder ‘PathFinding’ to anywhere you want your asset scripts folder. Once you have it use pathfinding like this:

  1. // create the tiles map
  2. float[,] tilesmap = new float[width, height];
  3. // set values here....
  4. // every float in the array represent the cost of passing the tile at that position.
  5. // use 0.0f for blocking tiles.
  6. // create a grid
  7. PathFind.Grid grid = new PathFind.Grid(width, height, tilesmap);
  8. // create source and target points
  9. PathFind.Point _from = new PathFind.Point(1, 1);
  10. PathFind.Point _to = new PathFind.Point(10, 10);
  11. // get path
  12. // path will either be a list of Points (x, y), or an empty list if no path is found.
  13. List<PathFind.Point> path = PathFind.Pathfinding.FindPath(grid, _from, _to);

If you don’t care about price of tiles (eg tiles can only be walkable or blocking), you can also pass a 2d array of booleans when creating the grid:

  1. // create the tiles map
  2. bool[,] tilesmap = new bool[width, height];
  3. // set values here....
  4. // true = walkable, false = blocking
  5. // create a grid
  6. PathFind.Grid grid = new PathFind.Grid(width, height, tilesmap);
  7. // rest is the same..