大家好,這次給大家分享最近學(xué)習(xí)A* 這款插件的心得,前面我一點(diǎn)點(diǎn)介紹,到最終我們實(shí)現(xiàn)一個多個小隊(duì)尋路的效果。
?首先介紹下怎么樣使用這個插件,下載插件后導(dǎo)入unity,新建個地面和障礙物,分別把Layer命名為Ground和Obstacles。在建議一個空物體,命名為A*,單擊Component-Pathfinding-Pathfinder 就可以看見Inspector面板出現(xiàn)了一個AstarPath,包含Graphs ,Settings,Save&load,Optimization和About 5個部分。 ? ? 點(diǎn)擊Graphs,添加一個新的Graph,這里就是添加我們的導(dǎo)航圖。這里有很多種類型,我們往后一個個介紹,先創(chuàng)建一個基于單元的導(dǎo)航圖,單擊Grid Graph,會生成一個width*depth的規(guī)則網(wǎng)格,將NodeSize設(shè)置1,表示節(jié)點(diǎn)之間的間距為1個單位,將Center的坐標(biāo)設(shè)置為(0,-0.1,0)因?yàn)槲覀兊牡孛鎦=0,如果GridGraph的y也為0,在投射射到平面時候,可能會產(chǎn)生浮點(diǎn)誤差,所以我們稍微下降一點(diǎn)高度。然后是CollisionTesting 碰撞測試選項(xiàng)里面,改變Mask的值,我們不希望AI在Obstacles層移動,所以設(shè)置Mask設(shè)置Obstacles層就可以了。接著是HeightTesting 高度測試,我們讓射線的長度為100,Mask設(shè)置Ground層,就是只在Ground層進(jìn)行投射,最后點(diǎn)擊最下方的Scan,就可以看到生成的網(wǎng)格了。
下一步,我們添加一個角色,把一個帶動畫的人物拖入場景,首先添加角色碰撞器,調(diào)整好屬性,選中人物,點(diǎn)擊Component-Pathfinding-Seeker 為角色添加seeker腳本,使角色能夠感知A*尋路,然后我們在創(chuàng)建一個移動腳本,讓角色能夠移動。為角色添加AstarAI.cs 腳本,
```using UnityEngine;
using System.Collections;
using Pathfinding;
public class AstartAI : MonoBehaviour {
? ?//目標(biāo)位置
? ?public Transform targetPosition;
? ?//聲明一個seeker類的對象
? ?private Seeker seeker;
? ?private CharacterController controller;
? ?//一個path類的對象。表示路徑
? ?public Path path;
? ?//角色每秒的速度
? ?public float speed = 100;
? ?//當(dāng)角色與一個航點(diǎn)的距離小于這個值時,角色便可轉(zhuǎn)向路徑上的下一個航點(diǎn)
? ?public float nextWaypointDistance = 3;
? ?//角色正朝其行進(jìn)的航點(diǎn)
? ?private int currentWaypoint = 0;
? ?void Start()
? ?{
? ? ? ?//獲得對Seeker組件的引用
? ? ? ?seeker = GetComponent();
? ? ? ?controller = GetComponent();
? ? ? ?//注冊回調(diào)函數(shù),在AstartPath完成尋路后調(diào)用該函數(shù)。
? ? ? ?seeker.pathCallback += OnPathComplete;
//調(diào)用StartPath函數(shù),開始到目標(biāo)的尋路
seeker.StartPath(transform.position, targetPosition.position);
}
private int turnSpeed = 100;
void FixedUpdate()
{
if (path == null)
{
return;
}
//如果當(dāng)前路點(diǎn)編號大于這條路徑上路點(diǎn)的總和,那么已經(jīng)到達(dá)路徑的終點(diǎn)
if (currentWaypoint >= path.vectorPath.Count)
{
Debug.Log("EndOfPathReached");
return;
}
//計(jì)算出去往當(dāng)前路點(diǎn)所需的行走方向和距離,控制角色移動
Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
dir *= speed*Time.fixedDeltaTime;
controller.SimpleMove(dir);
//角色轉(zhuǎn)向目標(biāo)
Quaternion targetRotation=Quaternion.LookRotation(dir);
transform.rotation=Quaternion.Slerp(transform.rotation,targetRotation,Time.deltaTime*turnSpeed);
//如果當(dāng)前位置與當(dāng)前路點(diǎn)的距離小于一個給定值,可以轉(zhuǎn)向下一個路點(diǎn)
if (Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance)
{
currentWaypoint++;
return;
}
}
//當(dāng)尋路結(jié)束后調(diào)用這個函數(shù)
public void OnPathComplete(Path p)
{
Debug.Log("FindThePath"+p.error);
//如果找到了一條路徑,保存下來,并且把第一個路點(diǎn)設(shè)置為當(dāng)前路點(diǎn)
if (!p.error)
{
path = p;
currentWaypoint = 0;
}
}
void OnDisable()
{
seeker.pathCallback -= OnPathComplete;
}
}
```
然后在新建一個小球,用來表示TargetPosition位置,小球放到我們想要移動到最終的目的地,然后拖到TargetPosition上,最后我們運(yùn)行場景,就可以看到我們的人物繞過障礙物進(jìn)行移動了,如果需要角色移動的平滑些,可以通過Component-Pathfinding-Modifiers添加,里面有很多種平滑腳本,我們選中SimpleSmooth 添加到角色上,然后慢慢調(diào)整參數(shù),就可以看到角色不同程度的平滑移動了。最后,提供工程,運(yùn)行Test1場景就行了。 ?