LayerMask problems

I’m using Physics.Raycast as such:

		LayerMask mask = LayerMask.NameToLayer("Block");
		mask = ~mask;
		
		if (Physics.Raycast(mouseRay, out hitInfo, 100000000.0f, mask))
		{
			Debug.Log (hitInfo.collider.tag);
        }

my intention is that the ray only hit things that are tagged as “Block”. However, I’m seeing other tags come up (World, Untagged, etc.), as well as Block.

Should the above not ignore anything that isn’t a Block?

LayerMasks are for layers, not tags.

–Eric

Try this pateras:

 LayerMask mask = LayerMask.NameToLayer("Block");

        mask = ~mask;

        

        if (Physics.Raycast(mouseRay, out hitInfo, 100000000.0f, mask))

        {

            Debug.Log ( LayerMask.LayerToName(hitInfo.collider.gameObject.layer));

        }

Oops, yeah, silly mistake looking at the tag.

Yeah, it’s still hitting all sorts of different layers. It logged 11 (Block, which is correct), 13, and 14, and printing out the names I can see that it’s hitting all sorts of objects that aren’t in the Block layer.

Found the problem (thanks to some help on reddit). The negation isn’t necessary (and is, in fact, the opposite of what I want to do), and, more importantly, I wasn’t doing the shift. This worked:

LayerMask mask = 1 << LayerMask.NameToLayer("Block");

if (Physics.Raycast(mouseRay, out hitInfo, 100000000.0f, mask))
{
  Debug.Log (hitInfo.collider.name);
}

Thank you both for your help.