I am willing to raycast specific layer and ignore other layers no matters its colider is above to my navPoint layer object?

 int specificLayerMask = LayerMask.GetMask("NavPoint");//raycast only this layer ignore others

        if (Physics.Raycast(raycastObject.transform.position, fwd, out objectHit, specificLayerMask))//50

the above code raycasting to navpoint layer but unable to bypass other layers colliders. is there any way available to ignore all other collider and raycast to desired layer only no matter colliders are behind any other collider

  • \$\begingroup\$ @DMGregory but sir my raycast is blocking by the collider \$\endgroup\$ Commented Apr 22, 2017 at 13:41
  • \$\begingroup\$ @MohammadFaizanKhan Yes, and DMGregory explained why and how you can fix it. \$\endgroup\$
    – Philipp
    Commented Apr 22, 2017 at 13:57
  • \$\begingroup\$ @DMGregory oh, silly me! thanks for pointing my mistake but can tell is this the right way to ignore other layers like this int ignoreLayer = 1 << 12; ignoreLayer = ~ignoreLayer; if (Physics.Raycast(raycastObject.transform.position, fwd, out objectHit, 100, ignoreLayer)) \$\endgroup\$ Commented Apr 22, 2017 at 15:01
  • \$\begingroup\$ If you have a question about selecting layers, it may be best to post this as a new question. Myself, I prefer to expose a LayerMask I can configure in the Inspector to ensure the intent is unambiguous and reduce magic numbers and bitshifts in my code, but if the line above works for you then go for it. You may want to AND it with the default raycast layers to ensure defaults you set elsewhere are respected. \$\endgroup\$
    – DMGregory
    Commented Apr 22, 2017 at 15:45
  • \$\begingroup\$ use bit shift answers.unity3d.com/questions/126285/… \$\endgroup\$ Commented Apr 23, 2017 at 15:35


Browse other questions tagged .