- Home /
SphereCast doesn't work with a layerMask
Or at least, as far as I can tell it doesn't. To test, I was doing a Raycast and a SphereCast in the same frame with exactly the same parameters (except the obvious addition of the range parameter to SphereCast), both with a layerMask var I set up as a public member variable and set in the editor. The SphereCast would fail even when the Raycast evaluated to true.
Try this code, and make sure you place a large object with a collider (like a cube or something) in front of the object you attach this to.
public var layerMask : LayerMask; //set this to something in the editor
public var sphereRadius : float = 10.0;
function Update(){
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, 1000, layerMask)){
Print("Raycast succeeded.");
}
else{
Print("Raycast failed.");
}
if(Physics.SphereCast(transform.position, sphereRadius, transform.forward, hit, 1000, layerMask){
Print("SphereCast succeeded.");
}
else{
Print("SphereCast failed.");
}
}
Or am I doing something wrong?
Answer by Bunny83 · Jan 25, 2012 at 02:52 PM
Keep in mind that if the sphere starts inside or partial inside the object you want to hit it won't detect an hit. A radius of 10 seems quite big (i don't know the object relative scales you use). What exactly do you want to detect?
btw. i guess the second Raycast should be a SphereCast ;)
In general keep in mind that MeshColliders can only be hit from one side (outside). Convex MeshCollider or any Primitive colliders are volume-colliders (can't have holes or single planes / trinagles). If a Raycast starts from the inside it does never hit the object.
edit
I've just realised you've used the LayerMask utility-class, but the wrong way ;)
The layermask is a bit-flag (32-bit integer value) where each bit represents one of the 32 layers. You have to use layerMask.value
in your Ray- / Spherecast.
Ah right, sorry. Fixed it to be a SphereCast as it should be. I'm casting from the camera to objects with capsule colliders in my actual game, which are 2 units tall. I've tried many different radii.
The fact remains, it works if I remove the layer$$anonymous$$ask parameter from the SphereCast call (and even check the layer of the hit object ins$$anonymous$$d, inside the SphereCast evaluation), so the radius is irrelevant. However it will always fail if the layer$$anonymous$$ask is used in the SphereCast call, even if the Raycast succeeded. Bug?
Hrm... that isn't true, at least for Raycasts. See Eric5h5's answer here (the last one): http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html
I've used it like that for almost 2 years now with Raycast and it absolutely works as intended. I'm guessing it just doesn't work with SphereCast for some reason.
I would say I'll try the .value method, but when you declare a public Layer$$anonymous$$ask member variable it creates a dropdown in the editor from which you could select multiple layers to be used in the mask. How would .value work in instances where I'm using a Layer$$anonymous$$ask with multiple layers?
Well regardless, layer$$anonymous$$ask.value works with SphereCast :) so I guess I'll just use that. but I will continue to use just layer$$anonymous$$ask in my raycasts out of spite, because it works dammit! :)
Yes, they implemented two implicit operators for Layer$$anonymous$$ask to convert a Layer$$anonymous$$ask to int and an int to Layer$$anonymous$$ask. I'm not sure why it shouldn't work with the implicit operator, but if something doesn't work the way you have it, you should stick to the documentation
Your answer
![](https://koobas.hobune.stream/wayback/20220613063359im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using Trigonometry for Collision Detection 0 Answers
Raycasting with LayerMask gives me a bce0023 1 Answer
Layermask doesn't seem to work 2 Answers
Problem in spherecast . 1 Answer
Layermask (raycast) wont work... 4 Answers