[Unity插件]A* Pathfinding Project:简易教程

原文链接:

题外话:最近想学习一下A*插件,由于在网上没有发现什么比较详细的教程,所以就只能上官网了。这是第一次看这么长的英语文章,翻译得不好,请见谅!

概述:

A*”astarpath.cs”。

第二重要的脚本就是”seeker.cs”,寻路的物体最好要有这个脚本

然后就是一个名叫"SimpleSmoothModifier.cs"的脚本,它可以让路径更加圆滑、简单,它要跟"seeker"脚本一起添加到同一个物体上。

1.创建一个新场景,创建一个”Ground”,把”Obstacles”。

2.创建一个新"AstarPath"脚本。脚本中最重要的是两个东西是“Graphs”和下方的“Scan”按钮。“Graphs”包含了所有的寻路图,最多可以有其中主要的两个是”GridGraph”(”Navmesh”(“Scan”按钮用来更新寻路图。

3.创建一个”GridGraph”,Gridgraph”Bottom-Left”,设为-0.1寻路图也是所以确保寻路图的y

高度检测

4.为了把寻路的。如果射线没有发生碰撞,说明检测的物体没有设置为“Ground”,又或者上面的第三点中

碰撞检测

5.当(。

添加AI

6.创建一个”Seeker”部件,Seeker)。接下来将会写一个脚本用于寻路,当然你也可以用自带的脚本。A*插件自带了两个主要适用于NavMesh类型。

让物体寻路

7.seek脚本中有一个重要的方法:functionStartPath(Vector3start,Vector3end,OnPathDelegatecallback=null):Path。参数是开始位置,结束位置,回调函数。其中OnPathDelegate是一个委托,被调用的函数类似于"voidSomeFunction(Pathp)"

脚本如下:

using UnityEngine;using System.Collections;//Note this line, if it is left out, the script won't know that the class 'Path' exists and it will throw compiler errors//This line should always be present at the top of scripts which use pathfindingusing Pathfinding;public class AstarAI : MonoBehaviour{public Transform target;public void Start(){//Get a reference to the Seeker component we added earlierSeeker seeker = GetComponent<Seeker>();//Start a new path to the targetPosition, return the result to the OnPathComplete functionseeker.StartPath(transform.position, target.position, OnPathComplete);}public void OnPathComplete(Path p){Debug.Log("Yay, we got a path back. Did it have an error? " + p.error);}}

把这个脚本添加到要寻路的物体上,,给target赋值,点击"Play"后,你应该会看到一条绿色的线,这就是寻路的路径了,此时物体还不会动。

如果没有看到绿线,检查一下"Seek"的脚本的"Show Gizmos"有无勾上。又或者可能是unity版本的原因,使Gizmos画出来的线被隐藏在plane下面了。

可以看到画出来的线很不圆滑,后面会进行美化的,先解释一下代码的意思。

(其实就是A*算法了,根据起点与终点的位置进行搜索,这里直接引用原文)

What happens is that first the script calls theSeeker’s StartPath function. The seeker will then create a new Path instance and then sent it forward to theAstarPathscript (the Pathfinder component you added before). TheAstarPathscript will put the path in a queue. As soon as possible, the script will then process the path by searching the grid, node by node until the end node is found.

Once calculated, the path is returned to theSeekerwhich will post process it if any modifiers are attached and then theSeekerwill call the callback function specified in the call. The callback is also sent towhich you can register to if you don’t want to specify a callback every time you call StartPath:

//OnPathComplete will be called every time a path is returned to this seekerseeker.pathCallback += OnPathComplete;//So now we can omit the callback parameterseeker.StartPath (transform.position,targetPosition);

注意上面的方法添加回调后,当你要移除或者摧毁该脚本,要: public void OnDisable () {seeker.pathCallback -= OnPathComplete;}当我们得到了计算后的路径,可以用它做什么?

计算后的路径有两个重要的list。

Path.vectorPath是一个Vector3的list,保存着每一个路径点的位置。

Path.path是一个node的list,保存着每一个路径点的node。

Path.vectorPath is a Vector3 list which holds the path, this list will be modified if any smoothing is used, it is the recommended way to get a path.secondly there is the Path.path list which is a list of GraphNode elements, it holds all the nodes the path visisted which can be useful to get additonal info on the traversed path.First though, you should always check path.error, if that is true, the path has failed for some reason. Path.errorLog will have more info on what went wrong in case path.error is true.

不要做刺猬能不与人结仇就不与人结仇,

[Unity插件]A* Pathfinding Project:简易教程

相关文章:

你感兴趣的文章:

标签云: