- Home /
c# layermask in raycasting doesn't work !!!
hi every body i used code below to make a ray only collide with Ground Layer and go through every other object. but doesn't work :
shadowRayMask=(1<<8); if (Physics.Raycast(transform.position, -Vector3.up, out hit,shadowRayMask.value))
any idea? Thanks
Answer by MojtabaM · Dec 01, 2013 at 12:52 PM
thank you for reply . The origin wasn't my problem because ray could draw to infinity that time. but i found i must set Distance in ray casting . when my ray created, it stops until it collide with first object .
so the code is : if (Physics.Raycast(transform.position, -Vector3.up, out hit,200, shadowRayMask)
Answer by SilentSin · Dec 01, 2013 at 10:56 AM
I'd say that hard coding 1<<8 is bad practice. How do you even know that 8 is the layer number you want?
Anyway, what I usually do is:
LayerMask shadowRayMask = 1 << LayerMask.NameToLayer("WhateverYourLayerIsCalled")
if (Physics.Raycast(transform.position, -Vector3.up, out hit, shadowRayMask))//No need for the .value here
Also keep in mind that this will raycast from the exact position of your object. So if the object's "pivot point" is at its base, then it could be just beneath the ground, meaning it will not hit anything. So you'd probably want to start at transform.position + Vector3.up or something.
Your answer
Follow this Question
Related Questions
Layer Mask Detection 2 Answers
Raycast layers 1 Answer
Why is RaycastHit2d Not working 0 Answers
Raycasting and Layer Masks problem 2 Answers
RaycastHit returns object without collider, in wrong layer 1 Answer