0
\$\begingroup\$

This question already has an answer here:

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

\$\endgroup\$

marked as duplicate by DMGregory, Philipp, Community Apr 24 '17 at 4:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

Browse other questions tagged or ask your own question.