Unity3D 官方文档 NavMesh三个组件的翻译与解释 自动寻路需要知道的细节

版本:unity 5.4.1  语言:C#

 

总起:

最近研究的ARPG游戏中,敌人AI的寻路就使用到了NavMesh,Unity该套组件倒也不复杂:三个组件和一个烘培的Map。我这边不讲基础怎么使用,主要是翻译一下各个组件的属性,说说需要注意的事情。主要参考就是官方文档和自己在Unity中的一些实践。

 

NavMesh中实现了A*算法作为底层的算法,使用NavMesh Agent可以作为角色的目标路径查找代理,而NavMesh Obstacle则是动态的一些障碍物,Off-Mesh Link作为特殊路径点的连接,并且可以与Animator动画组件实现经过特殊路径的动画,比如开门,连接门内门外的路径点,并执行Animator中的开门动画,这样的需求就可以用Off-Mesh Link来实现。

 

寻路三大组件:

NavMesh Agent

    NavMeshAgent用于角色的寻路代理,可以在预先烘培的NavMesh中找到目标的一条路线,并且尽可能的规避其他的Agent和Obstacle。(直接官方贴图拿来用了)



Radius  Radius of the agent, used to calculate collisions between obstacles and other agents.

agent的半径,用于计算该agent和其他agent、obstacle的碰撞。

 

Height  The height clearance the agent needs to pass below an obstacle overhead.

agent的高度,agent可以通过的头顶障碍最小高度。

 

Base offset  Offset of the collision cylinder in relation to the transform pivot point.

agent圆柱碰撞体中心点的偏移值。当Agent创建后,圆柱碰撞体显示过高或过低,用于将其调整到地面。

 

Speed  Maximum movement speed (in world units per second).

agent移动的最大速度(世界空间中以秒单位计算)

 

Angular Speed  Maximum speed of rotation (degrees per second).

agent最大的旋转速度(角度每秒)

 

Acceleration  Maximum acceleration (in world units per second squared).

agent最大的加速度(世界空间单位每秒的平方)

 

Stopping distance  The agent will stop when this close to the goal location.

agent靠近目标时停下的距离

 

Auto Braking  When enabled, the agent will slow down when reaching the destination. You should disable this for behaviors such patrolling, where the agent should move smoothly between multiple points

自动刹车(我总看成baking),当启用的时候agent会在到达终点时慢慢停下,你应该在角色需要平滑地巡逻几个固定点时,禁用它。

 

Quality  Obstacle avoidance quality. If you have high number of agents you can save CPU time by reducing the obstacle avoidance quality. Setting avoidance to none, will only resolve collision, but will not try to actively avoid other agents and obstacles.

障碍回避的质量值,如果你有大量的agent就可以降低该值节约CPU的资源,将它设置为0,则只会处理碰撞,不会尝试规避其他agent和obstacle。

 

Priority  Agents of lower priority will be ignored by this agent when performing avoidance. The value should be in the range 0–99 where lower numbers indicate higher priority.

agent会重视该值比自己小的,而忽略该值比自己大的。该值范围为0到99,数字越低意味着高权限,权限越高(越小的值)会被其他agent重视而回避。

 

Auto Traverse OffMesh Link  Set to true to automatically traverse off-mesh links. You should turn this off when you you want to use animation or some specific way to traverse off-mesh links.

需要agent自动通过off-mesh link的时候设置为true,当想要使用动画或其他特殊的方法通过off-mesh link时设为false。

 

Auto Repath  When enabled the agent will try to find path again when it reaches the end of a partial path. When there is no path to the destination, a partial path is generated to the closest reachable location to the destination.

启用时,agent会在到达局部路径的终点时,尝试再次寻找路径。当没有通向目标的路径时,它会生成一个离目标最近的点作为当前的一个目标。(我尝试下来发现,当目标点不可达,勾选该选项的agent尝试寻找临近目标点的欲望比不勾选的要大)

 

Area Mask  Area mask describes which area types the agent will consider when finding a path. When you prepare meshes for NavMesh baking, you can set each meshes area type. For example you can mark stairs with special area type, and forbid some character types from using the stairs.

AreaMask描述了agent会在哪些烘培的区域中寻找路径。当你准备烘培NavMesh时,你可以指定区域的类型,比如你想让阶梯作为一个特殊的区域,并且禁止某些角色使用阶梯。

 

   在使用Agent的时候,大家可能发现了Agent有Radius属性,在我们烘培NavMesh的时候设置的圆柱体也有Radius属性,两者有什么区别呢?

 

   在寻路的时候有两个阶段,第一阶段只考虑烘培NavMesh并寻找一条可行的路径,而第二阶段Agent就已经在前往目的地的路上了,它会考虑其他agent和obstacle,并尽量避开他们。

 

   烘培时候的Radius就对应第一阶段的生成NavMesh,而Agent上的Radius只在第二阶段使用。就比如说,有一条小径,你有一只大怪是通过不了该路径的,但烘培时候的Radius设置的比较小,虽然Agent的Radius可能设置的很大,但这只大怪还是会使用该小径通过。

 

   所以要理解寻路的两个阶段,再强调一遍,第一阶段没有开始真正的寻路不考虑其他的agent和obstacle,只看NavMesh,而第二阶段Agent已经在行走,而会尽量避开其他agent和obstacle。

 

Nav Mesh Obstacle

    NavMeshObstacle组件可以用于一些移动的障碍物,比如油桶箱子,Agent会尝试避开他们,当然这是在寻路的第二阶段。它有个特殊的功能雕刻(carve),它会在物体静止直接改变烘培的NavMesh,使Agent在第一阶段就会考虑到该障碍物。



Shape  The shape of the obstacle geometry. Choose whichever one best fits the shape of the object.

obstacle的几何形状,选择物体最合适的形状,有两种Box方块和Capsule胶囊体。

 

Box/Center  Center of the box relative to the transform position.

方块形状的中心点,与Transform的Position有关。

 

Box/Size  Size of the box.

方块的大小尺寸。

 

Capsule/Center  Center of the capsule relative to the transform position.

胶囊体形状的中心点,与Transform的Position有关。

 

Capsule/Radius  Radius of the capsule.

胶囊体的半径。

 

Capsule/Height  Height of the capsule.

胶囊体的高度。

 

Carve  When the Carve checkbox is ticked, the Nav Mesh Obstacle creates a hole in the NavMesh.

Carve该选项被勾选,Obsacle该物体会在NavMesh上制造一个空洞。

 

Move Threshold  Unity treats the Nav Mesh Obstacle as moving when it has moved more than the distance set by the Move Threshold. Use this property to set the threshold distance for updating a moving carved hole.

Unity会在Obstacle移动该距离下,将其处理为移动状态,使用该属性设置移动更新Carve空洞的频率。

 

Time To Stationary  The time (in seconds) to wait until the obstacle is treated as stationary.

Obstacle停止运动后,经过多少时间(秒)会被处理为静止状态。

 

Carve Only Stationary  When enabled, the obstacle is carved only when it is stationary. See Logic for moving Nav Mesh Obstacles, below, to learn more.

启用时,Obstacle仅会在静止时被处理成空洞,详见官方文档(废话)。这里有几个小细节需要注意,禁用该选项时,空洞的重新计算是隔一帧计算一次,就是两帧计算一次。然后最好启用该选项,因为每次空洞的重新计算比较费时。官方文档中举了两个适用的场景:木桶、箱子之类会受力停止的启用选项,而像坦克缓慢移动的,则可以禁用该项。

 

Off-Mesh Link

    OffMeshLink组件允许你在区域中给Agent提供一些小近路,使Agent能快速通过,比如跳过栅栏、通过一扇门。



Start  Object describing the start location of the Off-Mesh Link.

物体描述的开始点。

 

End  Object describing the start location of the Off-Mesh Link.

物体描述的结束点。

 

Cost Override  If value is positive, use it when calculating path cost on processing a path request. Otherwise, the default cost is used (the cost of the area to which this game object belongs). If the Cost Override is set to the value 3.0, moving over the off-mesh link will be three times more expensive than moving the same distance on a default NavMesh area. The cost override becomes useful when you want to make the agents generally favor walking, but use the off-mesh link when the walk distance is clearly longer.

如果该值是正的,使用它在处理需求路径时计算路径的代价值,否则会使用默认的代价值(游戏物体所属区域的代价值)。如果代价值设置超过3.0,经过该off-mesh link会比经过三倍的默认NavMesh区域还多。当你想用Agent初始化偏爱的路径时,该代价值非常有用。当然最好是在移动中清晰比较长的距离中使用。

 

Bi-Directional  If enabled, the link can be traversed in either direction. Otherwise, it can only be traversed from Start to End.

如果启用,则该link是双向的,否则只能从Start到End。

 

Activated  Specifies if this link will used by the pathfinder (it will just be ignored if this is set to false).

指定该link会不会在寻路的时候使用(在设置为false的时候被忽略)

 

Auto Update Positions  When enabled, the Off-Mesh link will be reconnected to the NavMesh when the end points move. If disabled the link will stay at its start location even if the end points are moved.

启用的时候,Off-Mesh Link会自动重新连接start和end的坐标,如果禁用,则只使用启用时候的坐标,即使后来坐标移动了也不会改变。

 

Navigation Area  Describes the navigation area type of the link. The area type allows you to apply a common traversal cost to similar area types and prevent certain characters from accessing the Off-Mesh Link based on the agent’s Area Mask.

描述link所属的NavMesh区域类型,该区域类型会允许你在相似类型区域中应用该区域的通路代价值,并且防止一些特定的Agent使用该通路,使用Agent的AreaMask属性来控制。

 

一个简单的脚本控制:

这边拿官方的一个脚本来看看怎么使用Agent的:

public class DH_CMyTPSController : MonoBehaviour 
{
    NavMeshAgent agent;
	void Start ()
    {
        // 获取NavMeshAgent
        if(agent == null)
            agent = GetComponent<NavMeshAgent>();
    }
	
	void Update ()
    {
	    if(Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            // 从屏幕发射一条射线,如果碰撞到物体,则会进入if内的代码
            if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
            {
                // 获取到鼠标点击的坐标,设置为Agent的目标
                agent.destination = hit.point;
            }
        }
	}
}

    使用静态物体搭建一个场景,然后烘培之后,创建一个角色(搞个胶囊体就好),添加Agent组件,把上面脚本放上去,就可以控制角色移动了,是不是很方便?

 

NavMesh和其他组件共用注意:

    Agent和Ridigbody,Ridigbody需要启用IK,否则会影响Agent的导航,因为在导航时Agent会自动控制角色移动,而不需要Ridigbody。

 

Agent和Animator,和上面的情况一样,Animator应该禁用Apply Root Motion,这样的使用非常简单粗暴。我们应该使用Animator来进行移动操作,而Agent只是预测作用,要使用这样的功能首先用代码禁用NavMeshAgent.updatePosition和NavMeshAgent.updateRotation,并使用NavMeshAgent.nextPosition获取预测坐标。

 

Agent和Obstacle,不要混用它们,它会回避自己导致错误,如果想要同时使用,请确保只有一个组件在Active状态。

 

Obstacle和Ridigbody,简而言之就是可以使用,或者说本来就该这么用。

 

总结:

    Unity提供的NavMesh使用起来还是比较简单方便的,和Animator进行配合能实现丰富的AI效果、甚至感觉做个RTS游戏也很方便。唯一不知道怎么搞的就是多场景下的NavMesh的连接,这个多场景我本身也没太理解。

 

Shader的博文有空会继续写,接下来的研究主要围绕这Unity的官方文档,一些重要的会写成博文给大家看看。同时会看官方出的一本书,预计使用1到2个月吧,弄的差不多了之后,想要做个游戏试试。

 

最后9月份准备自己的新生之路,哈哈。


  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值