NavMeshAgent 寻路导航组件

来源:互联网 发布:知乎 如何评价王尼玛 编辑:程序博客网 时间:2024/04/30 07:37

                                        NavMeshAgent 寻路导航组件


 
NavMesh地面的烘焙方法

1.选中要导航的模型 或者 地面
2.在U3D右边Inspector面板右上角Static旁边的倒三角 ,选中 Navigation static 勾,  表示地面导航层 ,把当前选中物体设置为导航层
3.菜单栏Window ->Navigation ,Inspector面板 右下角 Bake烘焙,生成导航路径数据
4.然后要移动的角色身上添加 NavMeshAgent组件,就可以导航移动角色

NavMeshAgent 组件面板属性




    Base offset                与地面偏移高度
    speed                        移动速度
    Angular Speed             转角速度 ,转身速度    角速度: 最高转速(度/秒)。
    Acceleration                 加速度,启动时的 最大加速度。
    Stopping Distance         停止距离 ,,制动距离:制动距离。到目的地的距离小于这个值,代理减速。
    Auto Traverse OffMesh Link 自动遍历OffMesh链接:自动移动并关闭OffMeshLinks
    Auto Repath                 自动重新寻路:如果现有的部分已失效,获得新的路径。
    Height                         高度:代理的高度(用于调试图形)。
    Base offset                   基本偏移:碰撞几何体相对于实际几何体垂直的偏移。
    Obstacle Avoidance Type 障碍躲避类型 :躲避的质量水平。
    NavMesh Walkable          导航网格行走:指定代理可以遍历的导航网格层类型。这个参数很有用,在接下来的实例中可以用到。


所有NavMeshAgent函数和变量翻译
NavMeshAgent.acceleration 加速度
NavMeshAgent.ActivateCurrentOffMeshLink 激活当前分离网格链接
NavMeshAgent.angularSpeed 角速度
NavMeshAgent.areaMask 区域遮挡
NavMeshAgent.autoBraking 自动制动
NavMeshAgent.autoRepath 自动重新获取路径
NavMeshAgent.autoTraverseOffMeshLink 自动穿过OffMeshLink
NavMeshAgent.avoidancePriority 逃避优先级
NavMeshAgent.baseOffset 基础偏移
NavMeshAgent.CalculatePath 计算路径
NavMeshAgent.CompleteOffMeshLink 完成分离网格链接
NavMeshAgent.currentOffMeshLinkData 当前关闭网格连接数据
NavMeshAgent.desiredVelocity 需求速度
NavMeshAgent.destination 目的地
NavMeshAgent.FindClosestEdge 寻找最近边缘
NavMeshAgent.GetAreaCost 获取区域成本
NavMeshAgent.hasPath 有路径
NavMeshAgent.height 高度
NavMeshAgent.isOnNavMesh 是否在导航网格上
NavMeshAgent.isOnOffMeshLink 是否在OffMeshLink上
NavMeshAgent.isPathStale 是否是旧路径
NavMeshAgent.Move 移动
NavMeshAgent.nextOffMeshLinkData 下一个OffMeshLink数据
NavMeshAgent.nextPosition 下个位置
NavMeshAgent.obstacleAvoidanceType 障碍逃避类型
NavMeshAgent.path 路径
NavMeshAgent.pathPending 路径等待
NavMeshAgent.pathStatus 路径状况
NavMeshAgent.radius 半径
NavMeshAgent.Raycast 射线投影
NavMeshAgent.remainingDistance 剩余距离
NavMeshAgent.ResetPath 重新设置路径
NavMeshAgent.Resume 恢复
NavMeshAgent.SamplePathPosition 样本路径位置
NavMeshAgent.SetAreaCost 设置区域成本
NavMeshAgent.SetDestination 设置目的地
NavMeshAgent.SetPath 设置路径
NavMeshAgent.speed 速度
NavMeshAgent.steeringTarget 转向目标
NavMeshAgent.Stop 刹车
NavMeshAgent.stoppingDistance 刹车距离
NavMeshAgent.updatePosition 更新位置
NavMeshAgent.updateRotation 更新旋转
NavMeshAgent.velocity 速度
NavMeshAgent.Warp 弯曲

NavMeshAgent 代码部分



注意事项
    1.启用组件,即使角色在站立状态,也会轻微为角色添加移动,非常小的
        但是会影响刚体的速度的属性
        最好开始移动时 启用NavMeshAgent,移动结束关闭NavMeshAgent

   //==============常用函数和变量================
    remainingDistance     //寻路剩余距离,角色和目标点的距离,(有时候为0,需要与Vector3.Distance()效验)
    stoppingDistance     //停止距离
    SetDestination(Point);//设置要移动的目标点
    destination          //设置要移动的目标点
    Stop();           //停止移动,寻路停止
    Resume          //Stop之后用Resume来恢复
    path.corners;//储存路径,路径坐标点数组
    ResetPath();  //重置路径
    pathStatus   //路径状况 ,当前路况的状态(完整,局部或者无效)。
        PathComplete    在目的地的路径终止。The path terminates at the destination.
        PathPartial        路径中不能到达目的地。The path cannot reach the destination.
        PathInvalid        路径无效。The path is invalid.
    velocity.magnitude //移动速度


//移动到B角色面前,用角色和B角色的包围盒的最近点,这样就不用修改 stoppingDistance停止距离
    agent.destination = target.collider.ClosestPointOnBounds(transform.position);

 //===================判断角色是否到达目标点
         if (moveState == MoveState.Moving)
        {
            Vector3 targetPoint = hitPoint;//目标点
            Transform moveT = transform;
            NavMeshAgent nma = GetComponent<NavMeshAgent>();
            float distance = Vector3.Distance(moveT.position, new Vector3(targetPoint.x, moveT.position.y, targetPoint.z));
            if (distance <= nma.stoppingDistance || nma.pathStatus==NavMeshPathStatus.PathComplete)
            {
                moveState == MoveState.Moving

            }
        }

//=======================停止移动==============
    void NavStop()
    {
        NavMeshAgent nma = GetComponent<NavMeshAgent>();
        if (nma.enabled == true)//停止NavMeshAgent ,停止移动
        {
            if (nma.hasPath)
            {
                nma.ResetPath();
                nma.Stop();
                nma.enabled = false;
            }
        }
    }

//=======================判断是否正在移动中======================
    public bool IsMoving() {
        // -> agent.hasPath will be true if stopping distance > 0, so we can't
        //    really rely on that.
        // -> pathPending is true while calculating the path, which is good
        // -> remainingDistance is the distance to the last path point, so it
        //    also works when clicking somewhere onto a obstacle that isn'
        //    directly reachable.
        return agent.pathPending ||
               agent.remainingDistance > agent.stoppingDistance ||
               agent.velocity != Vector3.zero;
    }
0 0