【Unity】問題記錄"SetDestination" can only be called on an active agent that has been placed on a NavMesh

問題:"SetDestination" can only be called on an active agent that has been placed on a NavMesh.

原因:導航NavMeshAgent組件所在物體距離Navmesh導航網格太遠,'NavMeshAgent.SetDestination'設置導航目標點失敗

解決:在設置導航目標點之前,可以用‘ NavMeshAgent.isOnNavMesh’判斷,在返回值爲Ture後再'NavMeshAgent.SetDestination',如果返回值爲False,說明當前NavMeshAgent不在導航網格上,可用‘NavMeshAgent.Warp’糾正位置。

此外,還可以用‘NavMesh.SamplePosition’檢測地圖內某點是否在導航網格NavMesh內

    public bool TestNavigation()
    {
        if (navMeshAgent.isOnNavMesh)
        {
            NavMeshHit navigationHit;
            if (NavMesh.SamplePosition(targetPosition, out navigationHit, 15, navMeshAgent.areaMask))
                return navMeshAgent.SetDestination(navigationHit.position);
            return false;
        }
        else
        {
            return navMeshAgent.Warp(warpPosition);
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章