unity 之Navigation 二

NavMesh Agent

NavMeshAgent 组件可以创建一个在NavMesh行走的角色,它们知道如何躲避和自己同类型的agent和障碍物

Properties

PropertyFunction
Agent Size
RadiusRadius of the agent, used to calculate collisions
between obstacles and other agents.
HeightThe height clearance the agent needs to pass below an obstacle overhead.
Base offset角色的collider的中心和agent的中心点的偏移,agent的中心点在下面,collider的中心在中间
Steering
Speed角色移动的最大速度 (in world units per second).
Angular Speed角色移动的最大角速度(degrees per second).
Acceleration角色移动的最大加速度(in world units per second squared).
Stopping distance角色距离目标点多远停下。默认是0
Auto Braking勾选上时,角色到达目的地自动停下.如果你想要在多个目的地之间巡逻,不勾选这个·
Obstacle Avoidance
Quality躲避碰撞物的质量,如果你有很多个agent,可以减少这个设置,来降低CPU消耗,如果设置为None,agent只会避免和碰撞体碰撞,而不会和agent和 obstacles发生碰撞
Priority低于这个优先级的agent,不会被该agent检测到,这个值在0-99之间,比如该agent的为80,则小于80的都不会和该agent发生碰撞
Path Finding
Auto Traverse OffMesh Link自动双向连接跳跃区域
Auto Repath当打开的时候,角色能从本地到大目的地,也能从目的地到达本地,谁双向的
Area Mask不能在没有勾选的上面行走

Details

The agent 通过一个向上的圆柱体检测 Radius and Height properties.agent的碰撞体会始终保持向上,不管它的transform有没有旋转.

.

当一个游戏物体的GameObject anchor point不和圆柱体(agent)的重叠)使用 Base Offset属性让他们重叠,就是让agent能够和圆柱体的外形相匹配

 agent的高和半径有两个设置的方式height and radius:

一个是在 NavMesh的窗口 NavMesh bake settings

另一个就是在 agent的组件上面设置属性.

  • NavMesh bake settings 设置的是所有的agent在静态物体发生碰撞或者是避免碰撞时的属性
  • NavMesh Agent properties 设置的是和动态的物体发生碰撞的属性,比如:移动的agent和移动的障碍物

大多数情况下,这两个值应该保持一致,但是也有例外,比如:一个很胖的士兵应该有很大的半径,这样其它的士兵的半径就很小,但是他们和静态物体发生碰撞的半径都是一样的

 

Nav Mesh Obstacle

Nav Mesh Obstacle 组件可以声明一个正在移动的障碍物 (例如,由物理系统控制的桶或板条箱). 当障碍物移动的时候, Nav Mesh Agents 会尽力躲避它. 当 obstacle 静止不动的时候,它在 NavMesh会雕刻一个洞,这样agent就不会考虑从当前的路径走


 

PropertyFunction
Shape代表碰撞体的形状.
    Box 
        CenterCenter of the box relative to the transform position.
        SizeSize of the box.
    Capsule 
        CenterCenter of the capsule relative to the transform position.
        RadiusRadius of the capsule.
        HeightHeight of the capsule.
Carve如果打开的话,障碍物会在navmesh上烘焙出一个洞来
    Move ThresholdNav Mesh Obstacle 游戏物体被当作障碍物必须移动的距离,小于这个距离不被当成移动的障碍物
    Time To Stationary障碍物停止多长时间后,被当成静止的障碍物
    Carve Only Stationary当打开的时候,仅仅在障碍物静止的时候,在导航网格上烘焙一个洞

Details

Nav Mesh Obstacles 通过两种方式影响agent移动:

Obstructing:阻碍

Carve 关闭的时候,默认障碍物就像一个  Collider,agent会躲避他,并与之发生碰撞,Nav Mesh代理可能无法在一个充斥着Nav Mesh障碍的环境中找到自己的路。此模式最适用于障碍物不断移动的情况(例如,车辆或玩家角色)

Carving

当打开 Carve时,障碍物会在静止时在网格烘焙一个洞,然后agent在规划路径的时候,就不会把这一块给考虑进去.

Logic for moving Nav Mesh Obstacles

Nav Mesh Obstacle当移动的距离超过 Carve > Move Threshold. 时认为Nav Mesh Obstacle 是移动的,这个时候烘焙的洞也是移动的,但是这样会消耗CPU,所以烘焙洞有两种方式来降低CPU消耗:

  • 第一个就是只有障碍物静止的时候才烘焙洞Nav Mesh Obstacle is stationary

  • 或者是当它移动的时候才烘焙洞Nav Mesh Obstacle has moved

Only carve when the Nav Mesh Obstacle is stationary

默认是打开Nav Mesh Obstacle 组件的Carve Only Stationary 选项.当Nav Mesh Obstacle已经停止移动并且静止的时间超过了 Carving Time To Stationary,将其视为静止,并再次更新被雕刻的hole。当导航网格障碍物移动时, the Nav Mesh Agents 使用碰撞体检测躲避他,即碰撞之后然后绕道.

Carve Only Stationary 对一些物理控制的碰撞比较好

Carve when the Nav Mesh Obstacle has moved

不勾选Carve Only Stationary ,当障碍物移动的距离超过设定的距离时,雕刻的洞就会更新Carving Move Threshold. 这种模式适用于大的、缓慢移动的障碍物(例如,被步兵避开的坦克)。

Off-Mesh Link

OffMeshLink 组件连接两个可行走的区域,让人物可以跳上或跳下

Properties

PropertyFunction
StartObject describing the start location of the Off-Mesh Link.
EndObject describing the start location of the Off-Mesh Link.
Cost Override跳跃的价值,如果价值过大,agent在寻路的时候,就不会有先考虑这一条路径
Bi-Directional双向跳跃
Activated路径查找器是否使用此连接
Auto Update Positions打开后,当终点移动的时候,路径也跟着变化,如果不打开,终点始终不变,注意,起点是永远不会变化的·
Navigation Area可以连接得区域

Details

连接得时候要确保连接区域是navigation mask可以连接得区域

 

 

Telling a NavMeshAgent to Move to a Destination

通过 NavMeshAgent.destination属性来设置目标点

  // MoveDestination.js
    var goal: Transform;

    function Start() {
      var agent: NavMeshAgent = GetComponent.<NavMeshAgent>();
      agent.destination = goal.position; 
    }

Moving an Agent to a Position Clicked by the Mouse

在update里面设置目标点为鼠标点击的地方,如果这个地方在NavMesh上的话

 if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                agent.destination = hit.point;
            }
        }

Making an Agent Patrol Between a Set of Points

 NPC在固定的点之间巡逻你可以保存一组点,让他在这之间按照一定的顺序来回巡逻

  迷宫的巡逻方式可以有很多种,这取决于你想怎么设置巡逻的顺序,比如:机器人可能按照一套顺序巡逻,而人类管理员可能是无规矩的巡逻,随机巡逻

 在 Update方法使用 remainingDistance 属性来获取和目标点之间的距离. 当距离很小的时候就可以巡逻下一地点了

    // Patrol.cs
    using UnityEngine;
    using UnityEngine.AI;
    using System.Collections;


    public class Patrol : MonoBehaviour {

        public Transform[] points;
        private int destPoint = 0;
        private NavMeshAgent agent;


        void Start () {
            agent = GetComponent<NavMeshAgent>();

            // 先关闭到大目的地就停下的属性
            agent.autoBraking = false;

            GotoNextPoint();
        }


        void GotoNextPoint() {
            // Returns if no points have been set up
            if (points.Length == 0)
                return;

            // Set the agent to go to the currently selected destination.
            agent.destination = points[destPoint].position;

            // Choose the next point in the array as the destination,
            // cycling to the start if necessary.
            destPoint = (destPoint + 1) % points.Length;
        }


        void Update () {
            // Choose the next destination point when the agent gets
            // close to the current one.
            if (!agent.pathPending && agent.remainingDistance < 0.5f)
                GotoNextPoint();
        }
    }


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TO_ZRG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值