2017-03-13 21 views
1

enter image description here防止移動到最近的可能位置

正如你可以在上面時,我的白路徑上單擊對象完全朝着點擊位置移動的圖片中看到。當我點擊藍色地面時,物體不會在那裏移動,但它會在白色路徑上找到最接近的可能位置,這是我不想要的行爲。

如果點擊在白色路徑之外,我希望對象不移動。

督察:

白路徑:靜態導航 - 步行遊覽

藍地:沒有。

對象腳本:

void Update() 
    { 
     if (Input.GetButtonDown("Fire1")) 
     { 
      Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
      RaycastHit hit; 
      if (Physics.Raycast(ray, out hit, 100)) 
      { 
       navAgent.destination = hit.point; 
       navAgent.Resume(); 
      } 
     } 
    } 

回答

3

我希望對象如果點擊是白 路徑之外不動了。

您可以通過檢查單擊哪個對象來做到這一點。您可以通過hit.collider.name的名稱檢查它,或者您可以使用hit.collider.CompareTag的標籤查看哪個對象被點擊。我建議你使用標籤。

創建一個名爲「whitepath」的標籤,然後將您的whitepath GameObject設置爲該標籤。然後,您可以比較raycast後的標籤名稱。 This是Unity如何創建標籤。

void Update() 
{ 
    if (Input.GetButtonDown("Fire1")) 
    { 
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
     RaycastHit hit; 
     if (Physics.Raycast(ray, out hit, 100)) 
     { 
      //Check for white path 
      if (hit.collider.CompareTag("whitepath")) 
      { 
       navAgent.destination = hit.point; 
       navAgent.Resume(); 
      } 
     } 
    } 
} 
+1

完美的答案。謝謝。 – Abdou023