2017-10-17 93 views
0

我正在使用GVRTeleport腳本來允許使用紙板應用進行傳送。我希望用於遠距傳物的射線能夠忽略除一層以外的所有層。通過this頁面我相應地修改了腳本(道歉,找不到原始鏈接到這個代碼的所有者),但是現在傳送腳本根本看不到任何東西。有任何想法嗎?我的底層是第8層,這是我希望這個raycast與之交互的層。Raycast到Unity中的特定圖層

using UnityEngine; 

public class GVRTeleport : MonoBehaviour { 

    public float viewHeight = 7f; 

    Vector3 fwd; 
    public float maxDistance = 10f; 
    public LineRenderer line; 
    public GameObject parent; 
    public GameObject targetIndicator; 

    public StraightLineParam genLine; 

    int layerMask = 1 << 8; 

    void Start() { 
    } 


    void Update() { 
     RaycastHit hit; 
     Ray ray; 

     if (Physics.Raycast (transform.position, Vector3.forward, Mathf.Infinity, layerMask)) { 
      Debug.Log ("The ray hit the floor"); 


      if (debugWithMouse) { 
       Vector2 mousePos = new Vector2 (Input.mousePosition.x/Screen.width, Input.mousePosition.y/Screen.height); 
       ray = Camera.main.ViewportPointToRay (mousePos); 
      } else { 
       ray = new Ray (transform.position, transform.forward); 
      } 

      if (Physics.Raycast (ray, out hit)) { 
       Debug.DrawLine (transform.position, hit.point, Color.red); 
      } 

      if (Input.GetMouseButton (0)) { 
       if (Physics.Raycast (ray, out hit)) { 

        if (useViewHeight) { 
         targetIndicator.transform.position = new Vector3 (hit.point.x, hit.point.y + viewHeight, hit.point.z); 
        } else { 
         targetIndicator.transform.position = new Vector3 (hit.point.x, hit.point.y, hit.point.z); 
        } 

        targetIndicator.transform.LookAt (hit.point); 
        targetIndicator.GetComponent<Light>().intensity = 8; 

        genLine.genLine (new Vector3 (ray.origin.x + 2, ray.origin.y - .5f, ray.origin.z), hit.point); 

        line.material.SetTextureOffset ("_MainTex", new Vector2 (Time.timeSinceLevelLoad * -4f, 0f)); 
        line.material.SetTextureScale ("_MainTex", new Vector2 (hit.point.magnitude, 1f)); 
       } 
      } 

      if (Input.GetMouseButtonUp (0)) { 
       if (Physics.Raycast (ray, out hit)) { 
        if (!debugNoJump) { 
         if (useViewHeight) { //better way? 
          parent.transform.position = new Vector3 (hit.point.x, hit.point.y + viewHeight, hit.point.z); 
         } else { 
          parent.transform.position = new Vector3 (hit.point.x, hit.point.y, hit.point.z); 
         } 
        } 
        if (!debugLine) { 
         line.SetVertexCount (0); 
        } 
       } 
       targetIndicator.GetComponent<Light>().intensity = 0; 
      } 
      Debug.DrawRay (this.transform.position, ray.direction * 5, Color.blue);// .DrawLine(transform.position, hit.point, Color.red); 
     } 
    } 
} 

回答

1

讓我們開始吧,您不應該在update()的每一幀中投射光線。不知道引擎如何反應。您應該先設置投射光線的條件,例如,當您按下某個鍵或類似鍵時。

然後,試試這個,而不是1 << 8的面膜:

int layer_mask = LayerMask.GetMask("Floor"); 

//Actually you can add any layer name you want, for example: 
//int layer_mask = LayerMask.GetMask("Ground","Enemy","Boxes"); 

//do the raycast specifying the mask 
if (Physics.Raycast (ray, out hit, distance, layer_mask)) 
{ 

} 

萬一你改變層的順序無意或修改它在未來,直接傳遞的名字,從我的觀點查看,這是更安全。

如果這不是問題,請檢查您正在使用哪個變換來投射射線的GameObject的前進方向。也許你只是在沒有任何東西的方向投射光線,或者至少不是地面。

+1

工作。我改變了一些其他的一些東西,將它整理並張貼分享。 – P1505C

+0

完美!聽起來不錯 –