2017-10-17 122 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

完美!听起来不错 –