- Home /
Differentiate between layers in LayerMask.
Context: I have a Raycast using a LayerMask (public variable). In the near future, I will have to select more than one layer in that LayerMask. Foreach layer selected ni my LayerMask my Raycast will return true.
Question: Is there any way I can identify wich layer did I hit?
PD: I know I can acces the hitted object layer (hit.collider.gameObject.layer). But I'm asking for a way to identify the layer extracting the info from the LayerMask variable.
Thank you all.
Answer by Bunny83 · Oct 25, 2018 at 06:28 PM
Your question is confusing. The layer mask that you specify when you perform a raycast is a setting. It's a value that is passed to the raycast method. It can not possibly be changed by the raycast method and wouldn't make any sense. This mask just specifies which layers are allowed to be hit. If you allow more than 1 layer you can not know what layer you may have hit unless you inspect the hit info. The RaycastHit struct is specifically designed to communicate information back to the caller. I don't get why you don't want to use this information.
Answer by SunnyChow · Oct 26, 2018 at 05:41 AM
i don't fullt understand your question either. the relationship between later and LayerMask is something like this
when layer is 3, a LayerMask with only layer 3 is 1<<3, which is 8. so if you want to do logic checking, you can do something like:
if((layerMask & (1<<layer)) >0)