I plan to make a shield to protect everything it covers by moving them to the IgnoreRaycastLayer.
Explosion is activated by SphereCastAll as following.
RaycastHit[] hits = Physics.SphereCastAll(position+(radius*Vector3.right), radius, (2*radius*Vector3.left), 2*radius, 557057);
While I try to check every thing I want to protect
RaycastHit[] shieldhits = Physics.SphereCastAll(ShieldTransform.position - ShieldTransform.up * ShieldRadius, ShieldRadius, 2 * ShieldRadius * ShieldTransform.up);
foreach (RaycastHit protection in shieldhits)
{
protection.rigidbody.gameObject.layer = Physics.IgnoreRaycastLayer;
}
But the SphereCastAll could still detect protected object.
I’m actually pretty positive and I know this sounds dumb and maybe i’m wrong
but i’d guess that raycast ignore only works for rays not capsules cast by sphere cast.
Double check to make sure the layer changed in the editor for selected objects though.
If you check and yes the layer does change for the objects I’d confirm rayignore doesnt work for sphere by doing
layer = physics.ignoreraycastlayer;
physics.spherecast(…,…, layer);
just to confirm
Uhm i think you have the wrong idea of the ignore raycast layer. It’s just an ordinary layer like any other layer. It’s just by default not included in the layermask. Since you pass a custom layermask it’s perfectly possible to raycast against the ignore raycast layer (layer 2).
Just have a look at the Physics class constants. The “IgnoreRaycastLayer” variable is a layermask that only contains layer 2 (so essentially the value 4 or 0x00000004). The “DefaultRaycastLayers” is the default layermask that is used when you do not provide your own. As you can see it’s simply the inverse of the IgnoreRaycastLayer mask. So it will contain all layers except layer 2. (== 0xFFFFFFFB)
You use a layermask of 557057
which is pretty strange to specify it in decimal. In hex it’s actually 0x00088001
. So you only cast against layer 1, 15 and 19 (the first layer is layer 0)
The issue you have is that you did not move your object onto the ignoreRaycast layer. The ignore Raycast layer is layer “2”. You set it to the “Physics.IgnoreRaycastLayer” value which is the layermask, not the layer index.
Yes the name “IgnoreRaycastLayer” is misleading as it should be named “IgnoreRaycastLayermask”. Though the documentation mentions that it’s a mask, not an index. On the other hand the GameObject.layer property takes a layer index as one object can only be on one layer at a time. The documentation also provides an example how to put an object onto the ignoreRaycast layer.
The correct code to set GameObject.layer
so that it ignores raycasts is:
gameObject.layer = 2;
Physics.IgnoreRaycastLayer
is the layer mask for the ignore raycast layer as opposed to the layer itself. Hence, Physics.IgnoreRaycastLayer = 1 << 2 = 4
. GameObject.layer
expects the layer itself, and not the corresponding mask.
References:
-
GamObject.layer
(look at the code example for putting game objects in the ignore raycast layer): https://docs.unity3d.com/ScriptReference/GameObject-layer.html
-
Physics.IgnoreRaycastLayer
: https://docs.unity3d.com/ScriptReference/Physics.IgnoreRaycastLayer.html
- Unity C# reference for
Physics.IgnoreRaycastLayer
: https://github.com/Unity-Technologies/UnityCsReference/search?q=ignoreraycastlayer&unscoped_q=ignoreraycastlayer