title: Raycast to specific layers in Unity
body: <p>I am using a GVRTeleport script to allow teleporting with a cardboard app. I want the raycast for teleportation to ignore all but one layer. Going through <a href="https://docs.unity3d.com/Manual/Layers.html" rel="nofollow noreferrer">this</a> page I have modified the script (apologies, can't find the original link to the owner of this code) accordingly, but now the teleport script sees nothing at all. Any ideas? My floor layer is layer 8, that's the layer I want this raycast to interact with.</p>
  
  <pre><code>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 &lt;&lt; 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&lt;Light&gt; ().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&lt;Light&gt; ().intensity = 0;
              }
              Debug.DrawRay (this.transform.position, ray.direction * 5, Color.blue);// .DrawLine(transform.position, hit.point, Color.red);
          }
      }
  }
  </code></pre>