title: Raycast passes through objects
body: <p>My raycast passes through objects with mesh collider/box collider with layers.</p>
  
  <p>I can't seem to find a solution. Please help me.</p>
  
  <p>I tried different solutions but the raycast still passes through objects. Even objects behind intractable objects are also being called out.</p>
  
  <pre><code> public float maxRayDistance = 2.0f;
  
   public LayerMask layerMask;
   public GUISkin mySkin;
   public bool showGui = false;
   public RaycastHit hit;
  
   void Update()
   {
       Vector3 dir = gameObject.transform.TransformDirection(Vector3.forward);
       if (Physics.Raycast(transform.position, dir, out hit, maxRayDistance, layerMask))
       {
           showGui = true;
           if (Input.GetButtonDown("Use"))
           {
               Invoke("SendAction", 0.3f);
           }
       }
       else
       {
           showGui = false;
       }
   }
  
   public void SendAction()
   {
       GameObject target = hit.collider.gameObject;                
       target.BroadcastMessage("Action");
   }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/mWh9p.png" rel="nofollow noreferrer">Layers</a></p>
  
  <p><a href="https://i.stack.imgur.com/XnBgE.png" rel="nofollow noreferrer">Object with Layer other</a></p>