# Raycast only specific layer and ignore other collider [duplicate]

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

• @DMGregory but sir my raycast is blocking by the collider – Muhammad Faizan Khan Apr 22 '17 at 13:41
• @MohammadFaizanKhan Yes, and DMGregory explained why and how you can fix it. – Philipp Apr 22 '17 at 13:57
• @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)) – Muhammad Faizan Khan Apr 22 '17 at 15:01
• 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. – DMGregory Apr 22 '17 at 15:45
• use bit shift answers.unity3d.com/questions/126285/… – Seyed Morteza Kamali Apr 23 '17 at 15:35