- Home /
Physics Raycast LayerMask problem?
Here's the code
public LayerMask lm;//I have already set this "cube" in Inspector
void Start () {
}
void Update () {
RaycastHit rayHit;
if (!Physics.Raycast(Camera.mainCamera.ScreenPointToRay(Input.mousePosition),100, out rayHit, lm))
return;
Debug.DrawLine(Camera.mainCamera.ScreenPointToRay(Input.mousePosition).origin, rayHit.point);
}
I have set two different layer: "cube" and "sphere",and I put two objects which change their layer one is "cube",the other is "sphere".When I test in Game , I found that both Objects can be hit by the ray.
So,is there something I set wrong?How can I just set it just hit the layer of "cube"?
Answer by Suyuanhan · Oct 12, 2011 at 03:45 AM
I found the problem: 1. The Raycast function have to need a parameter which is "distance" , or it won't select the layermask; 2. The "distance" parameter have to be at the place before "layermask" parameter,like: if (!Physics.Raycast(ray, out rayHit, 100, lm)) but if you set the wrong place like the code in my question,there is nothing wrong to Unity3d,but it just can't select the layermask.
Is it a bug?
ps:it just fine when you use layermask or layermask.value
Yeah, at second glance it appears that the arguments you passed in to Raycast don't match any of the function definitions exactly; you had the ray and distance arguments swapped.
The difference for me is that the raycast call in your original post does return an error when I try to compile it, saying that it doesn't match any definition of Raycast.
Right, the example does fit any existing function prototypes. One thing to watch for here is automatic type conversion from int (your layermask) to float (the distance). The compiler will happily accept something like Physics.Raycast(ray, out hit,~Layer$$anonymous$$ask.NameToLayer("$$anonymous$$yLayerName")) where the layer mask here gets used as the distance.
Answer by kromenak · Oct 11, 2011 at 06:57 PM
The Physics.Raycast function does not take a LayerMask variable, it takes an int variable. You can convert the LayerMask value to an integer value like this:
lm.value
As mentioned here: http://unity3d.com/support/documentation/ScriptReference/LayerMask.html