Unity A* Pathfinding Project Pro 教學.CG數位學習網

A* Pathfinding Project Pro 教學

A* Pathfinding Project 是由 Aron Granberg 開發的 Unity 外掛程式,功能強大但容易使用的尋路系統,提供 AI 人工智慧可以在複雜的場景(例如迷宮)找到玩家。適合用於製作塔防、第一人稱射擊,與即時策略遊戲。

官方網站 http://www.arongranberg.com/




A* Pathfinding Project 提供免費版本與專業版本,官方網站提供兩種版本,不過在 Unity 資源商店只提供了專業版本。

免費版本與專業版本的差異 https://www.arongranberg.com/astar/freevspro

A* Pathfinding Project 下載網頁 https://www.arongranberg.com/astar/download

A* 搜尋演算法 - https://en.wikipedia.org/wiki/A*_search_algorithm


線上文件 http://www.arongranberg.com/astar/docs/

離線文件 Documentation.zip 解壓縮即可瀏覽 A* Pathfinding 教學



Grid, navmesh and point graphs, so you've got 3 types of graphs included. Automatic navmesh generation to save you from doing it manually. Fully multithreaded so it will barely affect the frame rate. Path post-processing using raycasting, smoothing and using the funnel algorithm. A single line of code for a pathfinding call. Graphs can be saved to files. Local Avoidance both in the XZ and XY plane. Source code included. Supports updating graphs during runtime.

提供 16 個範例場景協助您開始使用,還有包含所有功能與變數的線上文件(英文)。


本教學使用 A* Pathfinding Project 4.1.12 版本講解,部份功能雖然受限無法使用,但功能仍然是相檔強大的。

02 圖形類型 (Graph Types)

原文網址 https://www.arongranberg.com/astar/docs/old/graph_types.php

專案包含多種不同的圖形類型,A* Pathfinding Project 甚至能允許開發者撰寫專屬的圖形類型,但是在我們的 A* Pathfinding Project 教學不對此部份討論。以下簡單說明這些圖形類型以及相關的設定:


Grid Graph 網格圖

The Grid Graph is the most straight forward graph. As the name implies it generates nodes in a grid pattern.

It works great for most scenes and is especially good when you need to update the graph during runtime (例如即時策略遊戲或塔防遊戲)。

It is not particularly good from a performance and memory point of view at handling large worlds with large open spaces however as it represents all regions with the same node density regardless of if they need that detail or not.

參考圖 https://www.arongranberg.com/astar/docs/old/images/gridgraph_graph.png

The grid graph can also be used as a hexagon graph if you set the 'Shape' option in the grid graph inspector to 'Hexagonal'. If you want to see an example you can take a look at the example scene called 'Example14_TurnBased_Hexagon'.

參考圖 https://www.arongranberg.com/astar/docs/old/images/grid_hexagon_thin.png


Navmesh Graph 導航網格圖

The Navmesh Graph is the other main graph type. This graph expresses the pathfinding data as a triangle mesh instead of squares (Grid Graph) or point (Point Graph).
This is perfect for smooth and fast pathfinding where the graph doesn't need much changing during runtime.
It is often faster than a grid graph since it usually contains fewer nodes and thus requires less searching.
The paths returned from it can be used directly, but the funnel modifier is strongly recommended.

The system can generate navmeshes automatically (see Recast Graph - A* Pro Only), but with this graph you will have to create them yourself in your favourite 3D modelling application.
A navmesh should be a mesh where the polygons describe the walkable area, vertices should always (except possibly in some special cases) lie at the edges of the mesh, not in the middle of it (i.e a vertex with polygons surrounding it).
It can also be good to split very long edges because similarly sized polygons yield better paths than really big and really small polygons next to each other.

參考圖 https://www.arongranberg.com/astar/docs/old/images/navmeshgraph_graph.png


Point Graph 點圖形

所有的圖形類型當中,點圖形 PointGraph 是最簡單的一種,但是可以進行許多客製化的設定。 it consists of a bunch of user placed points which are linked together. A point graph is scanned by taking a root Transform, and treating every child of it as a node. It then checks the connections between the nodes using raycasts to see if they should be linked together.
To get good, smooth paths from a point graph might be hard as they only define a point of walkability, not an area like the two previous graph types. The raycast modifier does a quite good job though.

A problem is that when getting the closest nodes for a path request, the closest node might be a node on the other side of a wall, so make sure you don't place your nodes too sparse.


專業版除了以上的類型,更支援 Recast Graph 與 Layered Grid Graph 類型,但在此 A* Pathfinding Project 教學暫時不涉及此部份。