Problem with LayerMask

I’ve added a LayerMask for a specific layer, to avoid hitting friendly teammates. But for some reason the LayerMask is making it ignore ALL collisions from ALL layers?

Any idea why this could be?

Could you post a sample of your code?
I think what you’re looking for is something like this:

// Bit shift the index of the layer (8) to get a bit mask
var layerMask = 1 << 8;
// This would cast rays only against colliders in layer 8.
// But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
layerMask = ~layerMask;

But I can’t tell for sure what’s wrong. I hope this helped.

if( Physics.SphereCast(transform.TransformPoint(new Vector3(0,0,2 ) ), // origin
		                       projectileRadius, // radius
		                       ( transform.position - lastPosition), // direction 
		                       out hit, // raycast hit
		                       2 + Vector3.Distance(transform.position, lastPosition), // distance
		                       ownerLayer) ) // LayerMask

ownerLayer is set to 8 (the layer that the player is in).

With this code it’s not colliding with either the player or anything else.

Have you made the

ownLayer=~ownLayer;

?
Then it would only be casting aginst everything,Except the layer 8. I assume that is what you want?
Please post the way you set the layer to 8. It would make it simplier to help.

I’m not sure what you mean by the code you posted.

Here’s the function I used to set the layer to 8. SphereScript is a reference to the projectile’s script

SphereScript.ownerLayer =  gameObject.layer;

the game object that script is on is placed on layer 8 (as are all friendly objects). I’ve confirmed in the Inspector that the layer is getting assigned correctly (and it is). And ownerLayer is an integer (as it says it should be in the SphereCast documentation).

Basically the script is intended to make the projectiles it shoots ignore collisions with all objects that share the same layer as it is on.

(Edit: I figured out what you may have meant from the code you posted, but if I set it to -ownerLayer it only hits self, and if I set it to ~ownerLayer it hits myself and terrain.

hm… Maybe you should try:

SphereScript.ownerLayer =  1 << gameObject.layer;
SphereScript.ownerLayer = ~SphereScript.ownerLayer;

So I can get it to hit ONLY self, nothing, or everything… what should be the proper syntax to make it only ignore the selected layer? Isnt that what ~ is supposed to do by default?

At the moment I’m getting this…

ownerLayer = ignore all

-ownerLayer = only hit self

~ownerLayer = collides with everything

“The ~ operator does this, it inverts a bitmask”. So If you have done 1 << 8 (which means hit only 8), then ~ means hit everything else except 8.

I must have made a mistake, the first time I tried the bit shift it did not work, but I tried again after your last post and it did work this time.

Thanks for your help =)

You’re welcome :slight_smile: