RayCast ignores All Layers, should ignore one layer

I have a problem. I created a Raycast.

RaycastHit hit; if (Physics.Raycast(muzzle[muzzleIndex].transform.position, muzzle[muzzleIndex].transform.forward, out hit, range, LayerMask.NameToLayer("Projectile")))
This Raycast is for a turret shooting projectiles, the raycast aims on the target. The Projectiles should not block the Raycast, so I assigned a layer to them. The problem is, that the raycast now ignores ALL layers, so it does not hit anything and the if-statement returns false. When I change one letter of the Layer (there is no layer with this name then), it works just fine, but of course it doesn’t ignore the projectiles. I tried everything I know, I use Debug.DrawLine, the Raycast is correct.

I have no idea what to do now

First of all, the layermask contains the layers you want to use, not the ones you want to ignore.

Second: LayerMask.NameToLayer() returns an index, but the mask contains one bit for each of the 32 possible layers.

Try this:

int layerMask = DefaultRaycastLayers & ~(1 << LayerMask.NameToLayer("Projectile") );


int layerMask = DefaultRaycastLayers & ~LayerMask.GetMask("Projectile");

NameToLayer returns the layer index, not a bitmask. You have to calculate that with bitwise operations:

RaycastHit hit; if (Physics.Raycast(muzzle[muzzleIndex].transform.position, muzzle[muzzleIndex].transform.forward, out hit, range, ~(1 << LayerMask.NameToLayer("Projectile"))))

or you can do it like the example here: LayerMask by setting the mask in the inspector.