2016-11-20 86 views
1

在我的場景中,我的NavMesh位於中心(黃色),其中3個立方體設置爲被跟蹤。我想讓NavMesh找到距離所有立方體最近的路徑並開始跟隨它。我已經寫了代碼來這樣做,但它行爲怪異,我沒有看到任何錯誤,但顯然有一些東西是。發生了什麼事情是,當我點擊遊戲並保持立方體原樣時,NavMesh確實找到了最接近的立方體的路徑並開始朝向它(立方體3),但是當它幾乎到達它時,導航網格很難轉向並開始朝向立方體1,這顯然不是更接近的立方體。使用NavMesh查找更近的玩家

enter image description here

這裏是我的代碼。 PathLength工作得很好。我認爲問題在於CalcPath函數。

float PathLength(Vector3 target){ 
    NavMeshPath path = new NavMeshPath(); 
    myNavMesh.CalculatePath (target, path); 
    int i = 1; 
    float currentPathLength = 0; 
    Vector3 lastCorner; 
    Vector3 currentCorner; 
    lastCorner = path.corners [0]; 

    while (i < path.corners.Length) { 
     currentCorner = path.corners [i]; 
     currentPathLength += Vector3.Distance (lastCorner, currentCorner); 
     Debug.DrawLine (lastCorner, currentCorner, Color.red,1f); 
     lastCorner = currentCorner; 
     i++; 
    } 

    return currentPathLength; 
} 


void CalcPath(){ 
    Vector3 closestTarget = Vector3.zero; 
    float lastPathLength = Mathf.Infinity; 
    float currentPathLength = 0; 

    foreach (GameObject player in GameObject.FindGameObjectsWithTag("Player")) { 

     currentPathLength = PathLength (player.transform.position); 

     if (currentPathLength < lastPathLength) { 
      closestTarget = player.transform.position; 
     } 

     lastPathLength = currentPathLength; 
    } 

    myNavMesh.SetDestination (closestTarget); 
} 

回答

2

您在CalcPath中遇到問題。我會試着舉個例子來告訴你什麼是錯的。說到球員的距離如下:[5,20,10]。顯然,玩家A是最接近的,但CalcPath將返回球員C.我會通過你的循環來說明爲什麼:

第一次迭代:
currentPathLength : 0 -> 5
closestTarget : null -> PlayerA.transform.position
lastPathLength : Mathf.Infinity -> 5

第二次迭代:
currentPathLength : 5 -> 20
closestTarget : PlayerA.transform.position -> PlayerA.transform.position(unc忌用)
lastPathLength : 5 -> 20

第三次迭代(這是你的問題所在)
currentPathLength : 20 -> 10(這是lastPathLength
closestTarget : PlayerA.transform.position -> PlayerC.transform.position
lastPathLength : 20 -> 10

若要解決此問題,而不是存儲lastPathLength,請存儲最小路徑長度,並且只有當您擁有新的最小值時才更改您的closestTarget

+1

你是絕對正確的。對我而言,lastPathLength是無用的。我想到了這一點,並張貼了我的答案,但我會在這裏保留我的答案並接受你的答案,因爲它解釋了發生的事情。也許它會幫助別人。感謝:D –

0

我將當前路徑長度與最後一個路徑長度進行比較,未存儲最短路徑並進行比較。我設置了一個名爲「closestTargetLength」的新變量,並將其與當前路徑長度進行比較,並且工作完美。

float PathLength(Vector3 target){ 
    NavMeshPath path = new NavMeshPath(); 
    myNav.CalculatePath (target, path); 
    int i = 1; 
    float currentPathLength = 0; 
    Vector3 lastCorner; 
    Vector3 currentCorner; 

    lastCorner = path.corners [0]; 
    while (i < path.corners.Length) { 
     currentCorner = path.corners [i]; 
     currentPathLength += Vector3.Distance (lastCorner, currentCorner); 
     Debug.DrawLine (lastCorner, currentCorner, Color.red); 
     lastCorner = currentCorner; 
     i++; 
    } 

    return currentPathLength; 
} 


void CalcPath(){ 
    Vector3 closestTarget = Vector3.zero; 
    float closestTargetLength = Mathf.Infinity; 
    float lastPathLength = Mathf.Infinity; 
    float currentPathLength = 0; 

    foreach (GameObject player in GameObject.FindGameObjectsWithTag("Player")) { 

     currentPathLength = PathLength (player.transform.position); 

     if (currentPathLength < closestTargetLength) { 
      closestTarget = player.transform.position; 
      closestTargetLength = currentPathLength; 
     } 
     lastPathLength = currentPathLength; 
    } 

    myNav.SetDestination (closestTarget); 
}