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();
}
}
}