NavMeshAgent 卡在(外部)NavMesh

NavMeshAgent stuck (outside) the NavMesh

对于敌人,我每隔 x 秒在导航网格上设置一个随机位置,以便敌人随机走动。这是我的脚本:

随机点(来自统一文档)

bool RandomPoint (Vector3 center, float range, out Vector3 result)
{
    for (int i = 0; i < 30; i++) {
        Vector3 randomPoint = center + Random.insideUnitSphere * range;
        NavMeshHit hit;
        if (NavMesh.SamplePosition (randomPoint, out hit, 1.0f, NavMesh.AllAreas)) {
            result = hit.position;
            return true;
        }
    }
    result = Vector3.zero;
    return false;
}

我的随机移动脚本:

IEnumerator Walking ()
{
    if (!isFollowingPlayer && isServer) {
        Debug.LogError ("-- Start Walking --");
        agent.speed = 2;
        Vector3 randomDirection;
        bool blocked = RandomPoint (agent.transform.position, walkRadius, out randomDirection);
        NavMeshHit hit;
        blocked = NavMesh.Raycast (enemy.transform.position, randomDirection, out hit, NavMesh.AllAreas);
        if (!blocked) {
            geileMethodeZumZeichnen (agent);
            target = hit.position;
            agent.SetDestination (hit.position);
            geileMethodeZumZeichnen (agent);
            Debug.LogWarning ("Go to " + hit.position);
        } 
        if (blocked) {
            Debug.LogWarning ("-- BLOCKED --");
            yield return new WaitForEndOfFrame ();
        } else {
            Debug.LogWarning ("-- WAIT --");
            yield return new WaitForSeconds (5f);
        }
        StartCoroutine ("Walking");
    }
}

随机游走一般有效。然而,当导航网格体代理靠近导航网格体的边界时,他似乎 "breaks out" 然后被困在他的位置上。他只 "jitters around" 在导航网格之外。 (图像上的红线:路径)

看起来他们试图到达他们的位置,但无法再次进入导航网格。我不希望他们首先离开:)

好的,最后这很简单: 我没有正确设置物理。由于物理原因,敌人真的 'felt' 进入边缘并卡在那里。现在工作起来很有魅力。