Drop Height:代理跳落高度。(只能往下跳,不可往上跳)。表示寻路者可以跳落的高度限。

Jump Distance:代理跳跃距离。(往远处跳跃的距离)。 表示寻路者的跳跃距离极限

01.勾选 Links

Navigation跳跃导航_寻路


02.设置跳跃的高度和距离

Navigation跳跃导航_寻路_02


03.NavMeshObstacle障碍物

Navigation跳跃导航_ide_03


Navigation跳跃导航_寻路_04


Navigation跳跃导航_ide_05


这个组件放在场景中的导航网格上,可以阻挡NavMeshAgent的移动。以Unity4.3.4为例。

NavMeshObstacle 分为两种模式,一种是普通模式,通过设置半径和高度来确定一个范围,阻档NavMeshAgent移动,一种是Carve模式,在导航网格上,根据模型的大小,挖出一个“洞”,使模型范围内的导航网格消失,这样NavMeshAgent就不能经过次区域了。

可以设置的属性如下:

Radius:半径

Height:高度

Move Threshold:当模式为Carve时,此物体的移动距离超过这个阀值后,更新当前的导航网格(从新挖洞)。

Carve:是否打开在导航网格挖洞的模式。

在Bake场景的时候,Navigation窗口的Bake页面有一个高度值,场景中的导航网格通常作为一个平面,当NavMeshObstacle 距离小于这个高度时,才会在导航网格上挖洞,否则NavMeshObstacle 还是以普通模式存在的。

NavMeshObstacle 在刚创建的时候最好先关闭NavMeshObstacle 这个组件,但需要是再打开,否则会有bug(先创建NavMeshObstacle,再Additive场景后,场景中一直存在一个不可通过区域)。创建完毕后,移动到适当位置再激活这个组件。

碰撞还是使用trigger

04.OffMeshLink跳跃点

Navigation跳跃导航_寻路_06


05.NavMeshComponents

导入NavMeshComponent,生成导航网

NavMeshSurface这个脚本将navmesh组件化,利用这个组件就可以很方便的烘焙挂载该组件的对象的navmesh信息,而无需打开一个navigation窗口对整个场景进行烘焙了。我们甚至可以将挂载这个脚本的GameObject烘焙后保存为一个prefab,这个带有navmesh信息的prefab跟其他的prefab一样。

Navigation跳跃导航_ide_07

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class BuildGameObject : MonoBehaviour
{

private NavMeshSurface navMeshSurface;
public GameObject perfab;
void Start()
{
navMeshSurface = this.GetComponent<NavMeshSurface>();
}


void Update()
{
if (Input.GetMouseButtonDown(1))
{
//使用主摄像机创建一根射线,射线的方向是鼠标点击的位置。
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//使用物理类检查射线的碰撞,如果点击物体存在
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Instantiate(perfab, hit.point, Quaternion.identity).transform.SetParent(transform);
navMeshSurface.BuildNavMesh();
}

}
}
}


05.自己控制

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Player : MonoBehaviour
{
private NavMeshAgent navMeshAgent;
public Transform target;
public float rotateSoomthing = 7f;
public float speed = 8f;
void Start()
{
navMeshAgent = this.GetComponent<NavMeshAgent>();
navMeshAgent.updatePosition = false; //NavMeshAgentd 的位置和旋转不受控制
navMeshAgent.updateRotation = false;
}

void Update()
{
if (Input.GetMouseButtonDown(0))
{
//使用主摄像机创建一根射线,射线的方向是鼠标点击的位置。
Ray ray =Camera.main.ScreenPointToRay(Input.mousePosition);
//使用物理类检查射线的碰撞,如果点击物体存在
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
navMeshAgent.nextPosition = transform.position;
navMeshAgent.SetDestination(hit.point);
}

}

Move();
}

public void Move()
{
if (navMeshAgent.remainingDistance<0.5f) //距离目标位置的距离 如果没有设置为0
{
return;
}
navMeshAgent.nextPosition = transform.position;
//设置朝向
transform.rotation=Quaternion.Lerp(transform.rotation,Quaternion.LookRotation(navMeshAgent.desiredVelocity), rotateSoomthing*Time.deltaTime);
transform.Translate(Vector3.forward*speed*Time.deltaTime);
}
}