導航系統

1.導航系統的簡單實現

實現倆個點之間讓物體自動移動

效果圖


圖1-1

1.先創建如圖1-1所示場景


圖1-2

2.物體層次分布如圖1-2所示 ? ? ? ? 設置left與right為圖1-3所示


圖1-3

3為player.添加Nav Mesh Agent組件設置對應值(圖1-4)

圖1-4

4.烘焙場景點擊Window-》Navigition到達如圖1-5所示箭頭指向部分全部打鉤


圖1-5

5.,在點擊到Bake界面出現如圖1-6所示根據需求設置

圖1-6

實現腳本

using UnityEngine;

using System.Collections;

public class playerMove : MonoBehaviour{?

?? public Transform[] targets;//目標??

? NavMeshAgent _Player_agent;?

?? int Index = 0;?

?? public void Awake()? ? {? ? ? ??

_Player_agent = GetComponent();

_Player_agent.speed = 4;

}

// Use this for initialization

void Start()

{

_Player_agent.destination = targets[Index].position;//尋路目標

InvokeRepeating("Patrol", 7f, 7f);

}

//巡邏方法

void Patrol()

{

if (_Player_agent.remainingDistance<0.5f)

{

if (Index < targets.Length - 1)

{

Index++;

}

else

{

Index = 0;

}

_Player_agent.destination = targets[Index].position;//尋路目標

}

}

}

6.把腳本掛在Player上在如圖1-7所示地方值改為2


圖1-7


7.會出現如圖1-8所示

圖1-8

將lefttager與righttager分別拖進去,表示的是倆個移動目標的點,運行后,物體會在這倆個點之間移動

NavMeshAgent重要屬性



圖1-9

二.實例二

效果圖(圖中4個物體會經過不同的路線去對面的高臺上在返回到原來的位置通過移動球體可以阻擋物體的移動)


圖2-1

1.設置倆快地板為如圖2-2所示

圖2-2

2.在有高臺的那塊地板上添加組件off Mesh Link如圖2-3移動行倆個空物體的位置(高臺與地面跳轉的倆個位置)拖進如圖中箭頭所指位置


圖2-3

3.添加烘焙場景點擊Window-》Navigition到達如圖2-4所示箭頭指向部分添加倆個層bridg1與bridg2


圖2-4

4.設置倆個橋的Navigation Area(層)分別為bridg1與bridg2如圖2-5


圖2-5

5.在Nav Mesh Agent 下設置層如圖2-6

圖2-6


圖2-7

6.為球體添加動態阻礙物,添加組件Nav mesh Obstacle如圖2-8(通過移動球體的位置可以阻礙物體的移動)

圖2-8

7.為物體添加腳本并將倆個位置拖進去

圖2-9

代碼實現

using UnityEngine;

using System.Collections;

public class play : MonoBehaviour{?

?? public Transform[] _play;??

? NavMeshAgent _nma;

? ? int Index=0;??

? public void Awake()??

? { ? ? ??

_nma = GetComponent();

_nma.speed = 4;

}

// Use this for initialization

void Start()

{

_nma.destination = _play[Index].position;

InvokeRepeating("A",7f,7f);

}

void A()

{

if (_nma.remainingDistance<0.5f)

{

if (Index < _play.Length - 1)

{

Index++;

}

else

{

Index = 0;

}

_nma.destination = _play[Index].position;

}

}

// Update is called once per frame

void Update()

{

_nma.destination = _play[Index].position;

}

}

三.實例三

實例圖(點擊按鈕會改變線路,使得橋進行切換,橋的設置如實例二一樣,添加倆個層分別使用不同的層)

圖3-1

在之前的基礎上,就是添加了畫線的功能

在腳本掛好如圖3-2所示后1與2為倆個位置,3與4為倆個橋


圖3-2

如圖3-3為物體添加Line Renderer組件如圖箭頭所指可以改變畫出來線的粗細


圖3-3

實現代碼1

using UnityEngine;

using System.Collections;

public class CubePath : MonoBehaviour{??

? NavMeshAgent _navMeshAg; ??

LineRenderer _lineRender;//畫線??

? NavMeshPath _NavMeshPath;? ? //? ?

?public void Awake()? ? {? ? ? ?

?_navMeshAg = GetComponent();? ? ??

? _lineRender = GetComponent();

_NavMeshPath = new NavMeshPath();

}

void Update()

{

DrawPath();

}

void DrawPath()

{

bool havaPath = _navMeshAg.CalculatePath(_navMeshAg.destination,_NavMeshPath);

if (havaPath)

{

Vector3[] pathCorners = _NavMeshPath.corners;

_lineRender.SetVertexCount(pathCorners.Length + 2);//路徑里的拐點不包含起始點

_lineRender.SetPosition(0, transform.position);//起點

?for (int i = 1; i <pathCorners.Length+1; i++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? _lineRender.SetPosition(i, pathCorners[i - 1]);

? ? ? ? ? ? }

? ? ? ? ? ? _lineRender.SetPosition(pathCorners.Length+1,_navMeshAg.destination);

? ? ? ? }

? ? }

}

實現代碼2

using UnityEngine;

using System.Collections;

public class CubeMove : MonoBehaviour{?

?? public Transform[] targets;?

?? NavMeshAgent _agent;

? ? float time = 0;?

?? public float timeInterVal = 2;?

?? int index = 0; ?

?public GameObject brige1;

? ? public GameObject brige2;

? ? public void Awake()??

? {? ? ? ?

?_agent = GetComponent();

}

void Start()

{

_agent.areaMask = 9;//Walkble brige1

brige2.transform.Rotate(0, 90, 0);

_agent.destination = targets[index].position;

}

void Update()

{

if (_agent.remainingDistance<0.5)

{

time += Time.deltaTime;

if (time>=timeInterVal)

{

time = 0;

index++;

index %= targets.Length;

_agent.destination = targets[index].position;

}

}

}

bool isBrige1_open = true;

bool isBrige2_open = false;

//按鈕開關函數

public void Brige1_Open()

{

if (!isBrige1_open)

{

_agent.areaMask = 9;//Walkble brige1

brige1.transform.localRotation = new Quaternion();

brige2.transform.Rotate(0, 90, 0);

isBrige1_open = true;

isBrige2_open = false;

}

}

public void Brige1_Close()

{

if (isBrige1_open)

{

_agent.areaMask = 17;

brige2.transform.localRotation = new Quaternion();

brige1.transform.Rotate(0, 90, 0);

isBrige1_open = false;

isBrige2_open = true;

}

}

public void Brige2_Open()

{

if (!isBrige2_open)

{

Brige1_Close();

}

}

public void Brige2_Close()

{

if (isBrige2_open)

{

Brige1_Open();

}

}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • Nav Mesh Agent 自動尋路(添加在移動的物體上) Off Mesh Link分離路面(添加在跳躍的起始...
    胤醚貔貅閱讀 292評論 0 0
  • title: 翻譯|React-navigation導航系統(2)date: 2017-03-28 07:48:3...
    smartphp閱讀 7,971評論 2 23
  • 秋風桂香濃 夜長霜露深 靜水浮弦月 風過竹影舞 閑步入寒園 幽思潛入眉 口念如來語 輕吟佛祖經 吾身本性善 也有齷...
    玫瑰圣典CC閱讀 143評論 0 0
  • 閱讀 喜歡上看書,那是高中時候的事了。剛進高中,不知怎么的,很不適應新環境,也沒什么朋友。適逢高中每個學期都有必讀...
    高曉松他侄子閱讀 323評論 1 5