1.導航系統的簡單實現
實現倆個點之間讓物體自動移動
效果圖
1.先創建如圖1-1所示場景
2.物體層次分布如圖1-2所示 ? ? ? ? 設置left與right為圖1-3所示
3為player.添加Nav Mesh Agent組件設置對應值(圖1-4)
4.烘焙場景點擊Window-》Navigition到達如圖1-5所示箭頭指向部分全部打鉤
5.,在點擊到Bake界面出現如圖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
7.會出現如圖1-8所示
將lefttager與righttager分別拖進去,表示的是倆個移動目標的點,運行后,物體會在這倆個點之間移動
NavMeshAgent重要屬性
二.實例二
效果圖(圖中4個物體會經過不同的路線去對面的高臺上在返回到原來的位置通過移動球體可以阻擋物體的移動)
1.設置倆快地板為如圖2-2所示
2.在有高臺的那塊地板上添加組件off Mesh Link如圖2-3移動行倆個空物體的位置(高臺與地面跳轉的倆個位置)拖進如圖中箭頭所指位置
3.添加烘焙場景點擊Window-》Navigition到達如圖2-4所示箭頭指向部分添加倆個層bridg1與bridg2
4.設置倆個橋的Navigation Area(層)分別為bridg1與bridg2如圖2-5
5.在Nav Mesh Agent 下設置層如圖2-6
6.為球體添加動態阻礙物,添加組件Nav mesh Obstacle如圖2-8(通過移動球體的位置可以阻礙物體的移動)
7.為物體添加腳本并將倆個位置拖進去
代碼實現
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-2所示后1與2為倆個位置,3與4為倆個橋
如圖3-3為物體添加Line Renderer組件如圖箭頭所指可以改變畫出來線的粗細
實現代碼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();
}
}
}