Unity3D navMesh如何判断到达目标点

1设置一个比较小的阈值,当前位置和targetPos之间距离小于该阈值,可认为到达终点。
2navMeshAgent.hasPath, 当有路径的时候为true,当到达终点或初始状态为false
3navMeshAgetn.path.corners.Length获得的是当前路径拥有的顶点数。当初始状态或者到达目标点后,获得的Length数为1;当>1的时候说明还没有到达终点。

需要注意的是:
当我们通过以下任一方式设置目标点后
navMeshAgent.destination=XXX
navMeshAgent.SetDestination(XXX)

navMeshAgent.path.corners和navMeshAgent.hasPath都不会立即刷新。必须等待一帧后才会刷新。因此使用法二或者法三进行判定的时候要特别注意。

//Unity导航是否到达目的地的方法,(包括有些不可能到达的目的地)
 public bool IsMoving()
    {
        if (!agent.enabled)
            return false;
        bool r = agent.pathPending ||agent.remainingDistance > agent.stoppingDistance ||agent.velocity != Vector3.zero;
        r = agent.enabled ? r : false;
        return r;
    }

以下是官方的一个多点寻路案例 里面也有判断如何到达目标点

 public class Patrol : MonoBehaviour {

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


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

            // Disabling auto-braking allows for continuous movement
            // between points (ie, the agent doesn't slow down as it
            // approaches a destination point).
            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();
        }
    }
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值