2017-04-15 173 views
0
void OnMouseDown() 
    { 
     if (Input.GetMouseButtonDown(0)) 
     { 
      RaycastHit hit; 
      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
      if (Physics.Raycast(ray, out hit, 1000.0f)) 
      { 
       int[] triangles = transform.GetComponent<MeshFilter>().mesh.triangles; 
       if (hit.triangleIndex != -1 && (hit.triangleIndex * 3) < triangles.Length) 
    } 

當我點擊網格上的其中一個三角形有兩個使用斷點的函數,但我看到,hit.triangleIndex是-1。爲什麼OnMouseDown函數的hit.triangleIndex返回-1?

在陣列三角形我有6項:

index 0 = 2 
index 1 = 1 
index 2 = 0 
index 3 = 2 
index 4 = 3 
index 5 = 1 

回答

2

三角指數纔有效如果被打的對撞機是 MeshCollider。

RaycastHit.triangleIndex

+0

權利,但問題出在我之前。直到現在我使用BoxCollider。我現在將其更改爲MeshCollider:gameObject.AddComponent ();現在的問題是,當我點擊網格上的鼠標時,它永遠不會進入OnMouseDown函數,只有當我檢查MeshCollider屬性名稱時:Convex。如果我檢查它設置爲true,它會到達OnMouseDown。但是當再次運行遊戲時,我需要將Convex設置爲false。每次我需要將Convex屬性更改爲true或false時,則不會進入OnMouseDown。任何想法爲什麼? –

+0

@DanielHalfoni一些Google搜索顯示[this](http://answers.unity3d.com/answers/996290/view.html)寶石。 – Iggy