U3D Navigation

让我们来一起粗步认识一下NavMesh的简单使用

 

首先我们建立一个新场景,在新场景我们创建 一个地形或者创建一个Plane,

然后在其上面用Cube或者其它的建立一些障碍物

再创建自己需要为其设置自动寻路的Cube,可以为它改个名字,我们在这里

将其改名为walker

再创建一个walker要自动到达的位置,创建一个Cube,改名为endposition;

 

准备工作做好了,让我一起来尝试下NavMesh

 

打开Navigation,Windows->Navigation

 

在Hierarchy层次窗口中将所有障碍物选上,在Navigation窗口,将Navigation Static打上勾,选择一个Navigation Layer 层

 

在Hierarchy层次窗口中将允许行进的地形或者Plane选上,做与上面一样的操作,注意将允许行进和不允许行进的Navigation Layer设置为不一样

 

选中walker为其添加NavMeshAgent组件  Component-> Navigation ->NavMeshAgent

组件属性就不介绍了,看得懂

唯一注意的就是在NavMesh Walkable中将不允许行进的NavigationLayer名字去除掉,将如这里不允许行进的Navigation Layer是Not Walkable

NavMesh Walkable就不应该将此层选中

 

在Hierarchy中将允许行进或者不允许行进的物体选中,在Navigation窗口中,选中Bake烘焙

为了让其运动,我们写一个简单的脚本,脚本内容如下

using UnityEngine;
using System.Collections;


public class Nav : MonoBehaviour {


// Use this for initialization
public Transform target;
private NavMeshAgent navmeshagent;
void Start () {
  navmeshagent = gameObject.GetComponent<NavMeshAgent>();
}

// Update is called once per frame
void Update () {
navmeshagent.SetDestination(target.position);

}
}

脚本不做解释,不懂的人查下文档

 

将脚本拖拉到walker中,再将endposition拖拉到上述脚本的target;

 

最后运行,完成,你会发现walker会自动寻找路线移动到endposition的位置。

posted @ 2013-07-22 11:55  凌霄神采奕奕  阅读(1117)  评论(0编辑  收藏  举报