In this tutorial we will learn about Navmesh in Unity, how to create it and use it on your game as part of navigation.

在本教程中,我们将学习有关Unity中Navmesh的知识,以及如何在导航中创建和使用它的方法。

Unity中的导航和寻路 (Navigation and Pathfinding in Unity)

Have you ever wondered how the various NPCs (Non-Playable Characters) in a game move around the game world, avoiding objects and even sometimes avoiding you, only to pop out from behind you to give you a jump scare. How is this done so realistically? How do these so called bots decide which paths they should take and which to avoid?

您是否曾经想过,游戏中的各种NPC(不可玩角色)如何在游戏世界中移动,避开物体,甚至有时避开您,而只是从您的身后突然冒出来,给您带来跳跃的恐惧。 如何现实地做到这一点? 这些所谓的漫游器如何决定应采取的路径以及应避免的路径?

In Unity, this miracle (not so much once you know how it works) is achieved using NavMeshes.

在Unity中,使用NavMeshes可以实现这个奇迹(一旦您知道它是如何运作的,就不那么多了)。

NavMeshes or Navigational Meshes are a part of the Navigation and path finding system of Unity. The navigation system allows you to create characters that can intelligently move around the game world, using navigation meshes that are created automatically from your Scene geometry. Dynamic obstacles allow you to alter the navigation of the characters at runtime, while off-mesh links let you build specific actions like opening doors or jumping down from a ledge.

NavMeshes导航网格是统一导航和寻路系统的一部分。 导航系统允许您使用从“ 场景”几何体自动创建的导航网格来创建可以在游戏世界中智能移动的角色。 动态障碍物使您可以在运行时更改角色的导航,而离网链接则使您可以建立特定的动作,例如开门或从窗台跳下。

NavMesh的不同组件 (Different Components of NavMesh)

The Unity NavMesh system consists of the following pieces:

Unity NavMesh系统由以下部分组成:

  • NavMesh (short for Navigation Mesh) is a data structure which describes the walkable surfaces of the game world and allows to find path from one walkable location to another in the game world. The data structure is built, or baked, automatically from your level geometry.

    NavMesh (导航网格的缩写)是一种数据结构,用于描述游戏世界的可行走表面,并允许查找游戏世界中从一个可行走位置到另一个位置的路径。 数据结构是根据关卡几何自动生成或烘焙的。

  • NavMesh Agent component help you to create characters which avoid each other while moving towards their goal. Agents reason about the game world using the NavMesh and they know how to avoid each other as well as moving obstacles.

    NavMesh Agent组件可帮助您创建在朝目标前进时彼此避开的角色。 代理商使用NavMesh来推理游戏世界,他们知道如何避免彼此以及移动障碍物。

  • Off-Mesh Link component allows you to incorporate navigation shortcuts which cannot be represented using a walkable surface. For example, jumping over a ditch or a fence, or opening a door before walking through it, can be all described as Off-mesh links.

    离网链接组件允许您合并导航快捷键,而这些快捷键无法使用可行走的表面来表示。 例如,跳过沟渠或栅栏,或者在穿过前先打开门,都可以描述为非网格链接。

  • NavMesh Obstacle component allows you to describe moving obstacles the agents should avoid while navigating the world. A barrel or a crate controlled by the physics system is a good example of an obstacle. While the obstacle is moving the agents do their best to avoid it, but once the obstacle becomes stationary it will carve a hole in the navmesh so that the agents can change their paths to steer around it, or if the stationary obstacle is blocking the path way, the agents can find a different route.

    NavMesh障碍物组件使您能够描述代理商在导航世界时应避免的移动障碍物。 由物理系统控制的桶或板条箱是障碍的一个很好的例子。 尽管障碍物正在移动,特工们会尽力避免它,但是一旦障碍物静止,它将在导航网格上刻一个洞,以便特工可以更改其路径以绕过它,或者如果静止的障碍物正在阻碍路径方式,代理商可以找到其他路线。

Courtesy: Untiy NavMesh Documentation

礼貌: Untiy NavMesh文档

Well all this is fine but this is all just theory. This just tells us what all tools exist. To know how to use them, that is an entirely a different ballgame altogether. To learn the implementation of these tools what’s better than an example?

嗯,这一切都很好,但这仅仅是理论上的。 这只是告诉我们所有工具都存在。 要知道如何使用它们,那完全是完全不同的方式。 要学习这些工具的实现,有什么比示例更好的?

时间为例! (Time for an Example!)

What we aim to do here, is that we wish to create a maze kind of scene, in which we will place our player. We will implement the NavMesh in such a way that on clicking a certain point in the maze, the player object will automatically start moving and reach the selected (clicked on) spot on the maze. Easy? Well, pretty much.

我们在这里要做的是希望创建一个迷宫般的场景,在其中放置玩家。 我们将以如下方式实现NavMesh :单击迷宫中的某个点后,播放器对象将自动开始移动并到达迷宫中选定(单击)的位置。 简单? 好吧,差不多。

设置和下载 (Setup and Downloads)

The components that we will be using are not included in the standard installation.

我们将使用的组件未包含在标准安装中。

Clone or download the repository from the download links below, on the Unity Technologies GitHub by clicking on the green Clone or download button. After that, open the NavMesh Components Project using Unity, or alternatively copy the contents of the

单击绿色的“克隆或下载”按钮,从下面的下载链接(在Unity Technologies GitHub上)克隆或下载存储库。 之后,使用Unity打开NavMesh组件项目,或者复制其中的内容。

Assets/NavMeshComponents folder to an existing Project.(Check unity version before downloading)

Assets / NavMeshComponents文件夹到现有项目。(下载前检查统一版本)

Unity 5.6 NavMesh Download

Unity 5.6 NavMesh下载

Unity 2017 NavMesh Downloads

Unity 2017 NavMesh下载

Unity 2018 NavMesh Download

Unity 2018 NavMesh下载

现场 (The Scene)

The scene that we will be using will look something like this. The green capsule is our player who we will try and move. The orange objects are the walls that we will avoid and the black object is the floor that we will walk upon. You can try to create this scene by yourself (do try). If not, click the link to download the scene.

我们将使用的场景将如下所示。 绿胶囊是我们将努力前进的球员。 橙色的物体是我们将要避开的墙壁,黑色的物体是我们将要走在其上的地板。 您可以尝试自己创建此场景(尝试)。 如果没有,请单击链接下载场景。

Download Scene //Place the correct link.

下载场景 //放置正确的链接。

烘焙NavMesh (Baking the NavMesh)

Baking? Is the Nav Mesh some kind of cake? Well.... in a way, yes. See, Nav Meshes are calculated and generated by the Unity engine. This process of calculation and generation of the Nav Mesh is referred to as Baking.

烤吗 Nav Mesh是一种蛋糕吗? 好吧……在某种程度上,是的。 请参阅,导航网格是由Unity引擎计算和生成的。 导航网格物体的计算和生成过程称为烘焙。

So what we need to do now is to bake our NavMesh.

因此,我们现在需要做的是烘焙NavMesh

  • To do this create an Empty Object in the Hierarchy Tab, name it “NavMesh”, and reset its transform (do it yourself).

    为此,在“层次结构”选项卡中创建一个“空对象”,将其命名为“ NavMesh”,然后重置其变换(自行完成)。

  • Select this “NavMesh” object in the Hierarchy and click on Components > Navigation > NavMeshSurface.

    在层次结构中选择此“ NavMesh”对象,然后单击“组件”>“导航”>“ NavMeshSurface”。

  • Now just to see the beauty of NavMesh, hit Bake.

    现在,只需看一下NavMesh的美丽之处,点击Bake。

  • On hitting bake, we get the above image. The blue region shows the surface that is walkable. The current bake of the NavMesh is not what we want. What we want is that only the flat part of the floor is marked blue. To make this happen, we will change the properties of the agent to match our Player. So, select the Agent Type drop down and click on Open Agent Settings.

    进行烘烤时,我们得到上面的图像。 蓝色区域显示可行走的表面。 导航网格的当前烘焙不是我们想要的。 我们想要的是只有地板的平坦部分标记为蓝色。 为此,我们将更改代理的属性以匹配我们的播放器。 因此,选择“代理类型”下拉菜单,然后单击“打开代理设置”。

  • Now in the Navigation tab, click on the + icon to create a new Agent Type and input the parameters as shown in the image.

    现在,在“导航”选项卡中,单击+图标以创建新的“代理类型”,并输入如图所示的参数。

  • Now clicking on Bake will have the desired effect. This is the path that we will need.

    现在单击“烘烤”将具有所需的效果。 这是我们需要的道路。

  • Now you will notice that the path treats our Player as an obstacle and hence there is no path under the Player. To remedy this we need to put our Player into another Layer and unmark the layer from the include layer setting. To do this click on the Layer drop down and select the Add Layer... option.

    现在,您会注意到路径将我们的玩家视为障碍,因此玩家下方没有路径。 为了解决这个问题,我们需要将Player放入另一个图层,并在包含图层设置中取消标记该图层。 为此,请单击“层”下拉菜单,然后选择“添加层...”选项。

  • Now in the Layer view, type in “Player Layer” in a free layer space and hit Enter.

    现在,在“图层”视图中,在可用图层空间中键入“ Player Layer”,然后按Enter。

  • After this, select the NavMesh object from the Hierarchy and in the inspector, click on the Include Layers drop down and uncheck the Player Layer.

    此后,从层次结构中选择NavMesh对象,然后在检查器中单击“包含层”下拉列表,然后取消选中“播放器层”。

    Now select the Player object in the Hierarchy, select the Layers drop down and click on “Player Layer”. We have thus changed the layer in which the Player object exists.

    现在,在“层次结构”中选择“播放器”对象,选择“层”下拉菜单,然后单击“播放器层”。 因此,我们更改了Player对象所在的层。

  • Now click on bake again to get the NavMeshWalkable Surface.

    现在再次单击“烘烤”以获取NavMeshWalkable曲面。

  • Now even though it seems that the walls are off limits, we have not ensured it. To ensure this we have to specifically make the walls “Not Walkable”. To do this first select all the wall objects from Wall (0) to Wall (9) in the Hierarchy, then select the Add Components drop down. From this drop down select Navigation > NavMeshModifier. Now all the wall objects have this component.

    现在,即使墙壁似乎无法通行,我们仍未保证。 为了确保这一点,我们必须专门将墙壁设置为“不可行走”。 为此,首先在层次结构中从Wall(0)到Wall(9)选择所有墙对象,然后选择“添加组件”下拉菜单。 从此下拉菜单中,选择导航> NavMeshModifier。 现在,所有墙对象都具有此组件。

  • With all the wall objects still selected, in the Inspector check the Override Area attribute on the Nav Mesh Modifier. Now select the Area Type drop down and select “Not Walkable”.

    在仍选择所有墙对象的情况下,在检查器中检查“导航网格”修改器上的“覆盖区域”属性。 现在,选择区域类型下拉菜单,然后选择“不可步行”。

  • Now finally click Bake again and bask in the beauty of the NavMesh you created.

    现在,最后再次单击“烘焙”,并享受您创建的NavMesh的美丽。

设置播放器 (Setting Up the Player)

Setting up the player is pretty easy. We just have to add the “NavMeshAgent” Component to the Player object.

设置播放器非常简单。 我们只需要将“ NavMeshAgent”组件添加到Player对象。

To do this:

去做这个:

  • Select the Player object in the Hierarchy.

    在“ 层次结构”中选择“ 播放器”对象。

  • In the Inspector, click on the Add Components drop down.

    在检查器中,单击“ 添加组件”下拉菜单。

  • From the drop down, select Navigation>NavMeshAgent.

    从下拉菜单中,选择导航>导航网格代理

We get this component attached to the player. Now our Player can access the NavMeshSurface. You can try and play around with the Steering and Obstacle Avoidance settings as they deal with the actual movement of our Player.

我们将此组件连接到播放器 。 现在我们的播放器可以访问NavMeshSurface了 。 您可以尝试使用“ 转向避障”设置,因为它们处理了我们播放器的实际运动。

使我们的球员移动 (Making Our Player Move)

To make our player move, we will attach a script to it, which will make the NavMesh move our player to the point on the floor that we click on.

为了使我们的播放器移动,我们将在脚本上附加一个脚本,这将使NavMesh将我们的播放器移动到我们单击的地板上的点。

To do this attach a new C# script, named “Controller”, to the Player object.

为此,将一个名为“ Controller”的新C#脚本附加到Player对象。

using UnityEngine;
using UnityEngine.AI;public class Controller : MonoBehaviour
{public NavMeshAgent playerAgent;void Update () {if (Input.GetMouseButtonDown (0)){Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);RaycastHit hit;if (Physics.Raycast (camRay, out hit)){playerAgent.SetDestination (hit.point);}}    }
}

Let us now understand the logic behind the code. Here what we wish to do is that get the position where the left mouse button was clicked, and casting out a ray from the camera to that point. The camera has a function called ScreemPointToRay(position),which shoots out a ray from the camera to the point given as input. Using rays, we can figure out the properties, components, attributes, etc. Of the object at the end of the ray.

现在让我们了解代码背后的逻辑。 在这里,我们要做的就是获取单击鼠标左键的位置,然后从相机投射光线到该点。 相机具有一个称为ScreemPointToRay(position)的功能,该功能可以从相机向指定的输入点发出光线。 使用射线,我们可以找出射线末端的对象的属性,组件,属性等。

Once the ray hits the floor we will get a true value in the if statement and we will set the player into motion toward the destination point. This is done using the SetDestination (Vector3) function of the NavMeshAgent class. This function moves the agent to the point given as parameter.

一旦光线射到地板上,我们将在if语句中获得一个真值,并将播放器移向目标点。 这是使用NavMeshAgent类的SetDestination(Vector3)函数完成的。 此功能将代理移动到作为参数给定的点。

Now after writing this code, in Unity, select the Player object from the Hierarchy, and in the Inspector, drag and drop the Player object into the playerAgent variable in the Controller script.

现在,在编写此代码之后,在Unity中,从层次结构中选择Player对象,然后在检查器中,将Player对象拖放到Controller脚本中的playerAgent变量中。

Now we are good to go! Go ahead press play! See the magic happen!

现在我们可以出发了! 继续按播放! 看到魔术发生!

This is just a simple maze. You can play with this. Create your own levels, bake the NavMesh and watch our player navigate the maze. Lastly and most importantly, have fun while making your game!

这只是一个简单的迷宫。 你可以玩这个。 创建自己的关卡,烘烤NavMesh,并观察我们的玩家在迷宫中导航。 最后也是最重要的是,在制作游戏时玩得开心!

导航网的力量 (Power of NavMeshes)

What we saw was just the absolute basic of using NavMeshes. NavMeshes can also be used during runtime.

我们看到的只是使用NavMeshes的绝对基础。 NavMeshes也可以在运行时使用。

For example take a floor with moving walls. The NavMeshSurface for this environment changes constantly. Thus runtime computation of NavMeshes is done (though it is extremely costly on resources; avoid).

例如,在地板上移动墙壁。 此环境的NavMeshSurface会不断变化。 这样就完成了NavMeshes的运行时计算(尽管这在资源上非常昂贵;避免使用)。

Also NavMeshes can be used for procedurally generated levels too. What we have to do is to just bake the NavMesh on Start. To do this we use the BuildNavMesh() function of the NavMeshSurface class.

NavMeshes也可以用于过程生成的关卡。 我们要做的就是在Start上烘焙NavMesh。 为此,我们使用NavMeshSurface类的BuildNavMesh()函数。

Thus we now have a tool, which is as powerful as it is simple to use. But remember, baking is processor heavy process, so use it wisely.

因此,我们现在有了一个功能强大且易于使用的工具。 但是请记住,烘烤是处理器的繁重过程,因此请明智地使用它。

翻译自: https://www.studytonight.com/game-development-in-2D/navigation-using-navmash

在Unity中使用NavMesh导航相关推荐

  1. 《UnityAPI.NavMesh导航网格》(Yanlz+Unity+SteamVR+云技术+5G+AI+VR云游戏+Unity+NavMesh+CalculatePath+立钻哥哥++OK++)

    <UnityAPI.NavMesh导航网格> 版本 作者 参与者 完成日期 备注 UnityAPI_NavMesh_V01_1.0 严立钻 2020.08.27 #<UnityAPI ...

  2. [专栏精选]Unity中动态构建NavMesh

    本文节选自洪流学堂公众号专栏<郑洪智的Unity2018课>,未经允许不可转载. 洪流学堂公众号回复专栏,查看更多专栏文章. 小新:"Unity内置的Navigation系统是不 ...

  3. 如何在Unity中实现AStar寻路算法及地图编辑器

    文章目录 AStar算法 简介 实现 Node节点 节点间的估价 算法核心 邻节点的搜索方式 地图编辑器 简介 实现 绘制地图网格 障碍/可行走区域 地图数据存储 AStar算法 简介 Unity中提 ...

  4. Cocos Creator3.x NavMesh导航网格寻路(一)

    前言 在游戏开发过程中,寻路可能是大多数游戏都必不可少的功能.2d游戏中最常用的就是A* 寻路了.在3d游戏中,对于一些简单的,没有高度地面A* 寻路同时也是可以使用的,但是对于一些地面比较复杂的游戏 ...

  5. Cocos Creator3.x NavMesh导航网格寻路

    前言 在游戏开发过程中,寻路可能是大多数游戏都必不可少的功能.2d游戏中最常用的就是A* 寻路了.在3d游戏中,对于一些简单的,没有高度地面A* 寻路同时也是可以使用的,但是对于一些地面比较复杂的游戏 ...

  6. 学习在Unity中创建一个动作RPG游戏

    游戏开发变得简单.使用Unity学习C#并创建您自己的动作角色扮演游戏! 你会学到什么 学习C#,一种现代通用的编程语言. 了解Unity中2D发展的能力. 发展强大的和可移植的解决问题的技能. 了解 ...

  7. Unity中使用WebView

    Unity中使用WebView @(设计) 需求,最近游戏中需要引入H5直播页面和更新比较频繁的赛事页面,需求包括:加密传参数.和Unity交互,在Unity框架下其实有几种方案: 内置函数Appli ...

  8. Faceware 面部捕捉在Unity中的应用

    官网软件下载:https://www.facewaretech.com/ 官网教程:http://support.facewaretech.com/ 官网素材下载:https://www.facewa ...

  9. unity3d学习笔记-动画(1.Unity 中动画的基本原理)

    一.创建动画 在 Unity 中,单个动画存储在一个名为Animation Clip的资源中.它们与创建剪辑时选择的对象相关联,并存储用于创建特定动画的所有数据.动画剪辑可以是任意时间长度,单个游戏对 ...

最新文章

  1. 使用maven构建Spring工程的一些重点
  2. java 获取网络带宽_Java开发网 - 一个测网络延迟和带宽的程序,发10M的数据有问题...
  3. 让pt-slave-restart支持MariaDB
  4. GraphPad Prism软件无响应问题解决办法
  5. VTK:vtkChartMatrix用法实战
  6. iOS中WebKit框架应用与解析
  7. 剑指Offer:面试题33——把数组排成最小的数(java实现)(未完待续)
  8. Eclipse添加JDK源码
  9. IIS无法启动——郁闷的KB939373补丁
  10. TypeError: 'module' object is not callable (pytorch在进行MNIST数据集预览时出现的错误)
  11. JS高级程序设计(10)
  12. 程序员为维持游戏开发被迫炒股,竟变成千万富翁
  13. 各国语言(语系)代码表(ZH-TW, ZH-CN,EN-US...) JSON 格式 [繁中/简中/英文格式]
  14. android 自定义快捷键,Android Studio中常用设置与快捷键
  15. python随机产生100个整数二进制_python产生随机整数
  16. usb key 开发(一)
  17. MCU学习——无线遥控模块
  18. 【C语言】C语言之数字金字塔全家桶
  19. py实战绘制人口金字塔图
  20. Python:使用PyAV提取视频关键帧

热门文章

  1. JavaScript 23 种设计模式之 4 抽象工厂模式
  2. matlab图像水印,怎么在含有水印的图像中提取出水印
  3. 【密码效验最强正则表达式】
  4. 【WY】数据分析 — Numpy 阶段一 :基础语法 一 —— 介绍
  5. python 内置属性__setattr___python魔法方法之__setattr__()
  6. 基于TensorFlow的VGG16模型源码
  7. Go1.8 泛型简单上手使用
  8. 生成环境(pro),开发环境(dev)的理解,--save -dev的理解
  9. 环保数采仪助力空气质量在线监测系统
  10. C++入门——构造函数和析构函数