最近在研究导航功能,正好可以写几篇笔记。 在Unity中,NavMeshAgent组件负责实现导航功能,面板属性如下:[

](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080458-217x300.png)](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080458.png) 几个常用的属性: Radius[半径]:控制代理器这个“圆柱体”的半径; Height[高度]:控制代理器这个“圆柱体”的高度; Base Offset[基础偏移]:控制代理器这个“圆柱体”在垂直方向的偏移量; Speed[速度]:导航的移动速度; Angular Speed[转弯速度]:模型是Y 轴朝向目标点;如果不是,则转向; Acceleration[加速度]:保持默认即可; Stopping Distance[停止距离]:距离目的地多远的时候停止; Auto Braking[自动停止]:保持默认勾选状态即可; 下面开始实战: 1.新建一个plane,设置为static,即静态物体[![](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080459-300x176.png)](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080459.png) 2.打开Window下的Navigation面板,选中Bake,点击Bake,烘焙导航场景,可以看到Plane上加了一片蓝颜色区域,这就是可移动区域。[![](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080460-220x300.png)](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080460.png)[![](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080461-300x146.png)](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080461.png) 3.新建一个Cube放在Plane上面,给Cube添加NavMeshAgent组件,新建一个Sphere,也放在Plane上面[![](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080462-300x146.png)
](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080458-217x300.png)](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080458.png) 几个常用的属性: Radius[半径]:控制代理器这个“圆柱体”的半径; Height[高度]:控制代理器这个“圆柱体”的高度; Base Offset[基础偏移]:控制代理器这个“圆柱体”在垂直方向的偏移量; Speed[速度]:导航的移动速度; Angular Speed[转弯速度]:模型是Y 轴朝向目标点;如果不是,则转向; Acceleration[加速度]:保持默认即可; Stopping Distance[停止距离]:距离目的地多远的时候停止; Auto Braking[自动停止]:保持默认勾选状态即可; 下面开始实战: 1.新建一个plane,设置为static,即静态物体[![](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080459-300x176.png)](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080459.png) 2.打开Window下的Navigation面板,选中Bake,点击Bake,烘焙导航场景,可以看到Plane上加了一片蓝颜色区域,这就是可移动区域。[![](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080460-220x300.png)](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080460.png)[![](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080461-300x146.png)](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080461.png) 3.新建一个Cube放在Plane上面,给Cube添加NavMeshAgent组件,新建一个Sphere,也放在Plane上面[![](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080462-300x146.png)
4.新建一个C#脚本PlayerGo放在我们的Cube身上 脚本内容如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;//引入AI,才能操作导航系统的组件
public class PlayerGo : MonoBehaviour
{
private NavMeshAgent m_NavMeshAgent;//定义Cube身上的NavMeshAgent
public Transform m_Transform; //公开定义我们的Sphere
// Use this for initialization
void Start ()
{
m_NavMeshAgent = gameObject.GetComponent();
}

// Update is called once per frame
void Update () {
    //方法:SetDestination(目标点位置);
    //m\_NavMeshAgent.SetDestination(m\_Transform.position);

    //属性:destination = 目标点位置;
    m\_NavMeshAgent.destination = m\_Transform.position;

    //18行代码和21行代码作用一样,都是引导目的地的位置
}

}

5.找到Cube身上的脚本,把Sphere拖进去[

](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080463-300x161.png)](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080463.png) 6.运行测试:下图为GIF图,点击图片查看动态效果:[![](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080464-300x209.gif)
](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080463-300x161.png)](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080463.png) 6.运行测试:下图为GIF图,点击图片查看动态效果:[![](http://www.wjgbaby.com/wp-content/uploads/2017/08/17080464-300x209.gif)