Unity3D尋路導航NavMesh——自動尋路過吊橋
阿新 • • 發佈:2019-02-08
Unity中對於尋路導航演算法進行了封裝,支援提交將地圖進行導航烘焙
如果想在兩個不連續的平面之間加一個吊橋,在橋放平時可以使物體通過吊橋,而橋擡起時物體在橋邊等待。
1. 給橋的加一個旋轉中心如圖中藍色球體,將橋和地面分開,設定兩個地面的Navigation值如圖,勾選Navigation Static和Generate OffMeshLinks,Navigation Area的值設為Walkable
2. 設定橋體的Navigation值如圖,同樣勾選Navigation Static和Generate OffMeshLinks,如圖在Areas新增標籤bridge,並將Navigation Area的值設為bridge
3. 給物體1新增元件Navigation->Nav Mesh Agent
4.給橋新增指令碼程式碼如下
private Transform Sphere;
private bool isOver;
private GameObject src;
// Use this for initialization
void Start ()
{
isOver = false;
Sphere = GameObject.Find("Sphere").transform;
transform.RotateAround(Sphere.position, Vector3.left, 45 );
src = GameObject.Find("src");
}
// Update is called once per frame
void Update ()
{
if(!isOver&&Time.realtimeSinceStartup > 5)
{
transform.RotateAround(Sphere.position, Vector3.left, -45);
isOver = true;
src.SendMessage("SendMask" , -1);
}
}
5.給物體1新增指令碼
public Transform dest;
private NavMeshAgent agent;
// Use this for initialization
void Start ()
{
agent = GetComponent<NavMeshAgent>();
agent.SetDestination(dest.position);
agent.areaMask = -1 ^ 1 <<6;//第六個標籤
}
void SendMask(int mask)
{
agent.areaMask = mask;
}