2017-11-09 35 views
0

最近、AIとUnityの経路探索機能を使って遊んできました。これまでのところ私はそれに大きな問題を抱えていませんでしたが、昨日私は山のモデルをその土台から上に行く道で輸入しました。UnityのNavMeshエージェントがnavmeshのエッジでグリッチする

私のAIゲームオブジェクトは、剛体(キネマティックに設定)、カプセルコライダー、NavMeshエージェントで構成されています。 私はNavMeshを生成し、最大勾配を40度に設定しました。プレイヤーに従ったり、指定されたパス(ウェイポイント)に沿って歩いているときにAIがうまくナビゲートしますが、AIメッシュの半径でNavMesh上のランダムポジションを選ぶとき、通常、目的地がNavMeshの端に近い場合、数秒後に揺れ始めます。 私はそれの2つのビデオをキャプチャしており、私は昨日からこれを修正するために苦労しています。

障害物回避設定を変更して最大勾配を下げましたが、これまでのところ何もできませんでした。

これは、それがどのように見えるかです:

Video 1

Video 2

私はランダムな位置を取得するために使用するコードを:

Pastebin

 void ControlRandomWander() 
     { 
      float pointDist = Vector3.Distance(currentWanderPos, transform.position); 

      if(pointDist < 2f || currentWanderPos == Vector3.zero) 
      { 
       wanderWaitTimer += Time.deltaTime * 15; 

       anims.LookAround(true); 

       if (wanderWaitTimer >= wanderWaitTime) 
       { 
        Vector3 randPos = GetRandomPositionAroundTarget(transform.position, -wanderRadius, wanderRadius); 

        NavMeshPath pth = new NavMeshPath(); 
        NavMesh.CalculatePath(transform.position, randPos, agent.areaMask, pth); 
        float pathDist = 0f; 

        if (pth.status == NavMeshPathStatus.PathComplete) 
        { 
         for (int i = 0; i < pth.corners.Length - 1; i++) 
         { 
          pathDist += Vector3.Distance(pth.corners[i], pth.corners[i + 1]); 
         } 

         Debug.Log(pathDist); 

         if (pathDist <= wanderRadius) 
         { 
          currentWanderPos = randPos; 

          wanderWaitTime = Random.Range(wanderWaitMin, wanderWaitMax); 

          anims.LookAround(false); 
          wanderWaitTimer = 0; 

          MoveToPosition(randPos, true); 
         } 
        } 
       } 
      } 
      else 
      { 
       if (agent.destination != currentWanderPos) 
       { 
        MoveToPosition(currentWanderPos, true); 
       } 
      } 
     } 

     Vector3 GetRandomPositionAroundTarget(Vector3 pos, float minRange, float maxRange) 
     { 
      float offsetX = Random.Range(minRange, maxRange); 
      float offsetZ = Random.Range(minRange, maxRange); 

      Vector3 orgPos = pos; 

      orgPos.x += offsetX; 
      orgPos.z += offsetZ; 

      NavMeshHit hit; 
      if(NavMesh.SamplePosition(orgPos, out hit, 5f, agent.areaMask)) 
      { 
       Debug.Log(hit.position); 
       return hit.position; 
      } 

      Debug.Log(hit.position); 

      return pos; 
     } 

私は本当にappreciatだろうこの問題を整理するのに役立ちます。

答えて

0

NavMesh.SamplePositionは、エージェントが到達できないようにNavMesh Agentの半径を考慮していません。

私は、エージェントの半径の半分だけhit.positionをエッジから動かすことで修正することができました。

if(NavMesh.SamplePosition(orgPos, out hit, 5f, agent.areaMask)) 
{ 
    Vector3 ret = hit.position; 
    Vector3 pathDir = pos - ret; 
    ret += pathDir.normalized * (agent.radius/2); 

    return ret; 
} 
関連する問題