Make raycast ignore a layer?
I have been looking about the forums but none of the other answers seem to be any help. I created a layer that I want one of my raycasts to ignore.
private int layerMask = 1 << 9;
Ray pointerRaycast = new Ray(transform.position, transform.forward);
RaycastHit pointerCollidedWith;
var rayHit = Physics.Raycast(pointerRaycast, out pointerCollidedWith, layerMask);
if (pointerCollidedWith.transform.tag != "Wall" && ObjectProperties.WallObject == false)
{
lastInstantiated.transform.position = new Vector3(pointerCollidedWith.point.x, pointerCollidedWith.point.y, pointerCollidedWith.point.z);
} else if(pointerCollidedWith.transform.tag != "Floor" && ObjectProperties.WallObject == true)
{
lastInstantiated.transform.position = new Vector3(pointerCollidedWith.point.x, pointerCollidedWith.point.y, pointerCollidedWith.point.z);
}
{
}
For some reason this code isn't doing anything?
$$anonymous$$ake sure the objects you want to ignore also have that specific layer as their layer. You can also try going to Project Settings > Physics and mess with the checkmarks at the bottom in the Inspector for collision between layers.
Answer by EDevJogos · Aug 15, 2016 at 01:27 AM
use the operator ~ if you want the mask to ignore a specific layer.
private int layerMask = ~(1 << 9);
How do you write that to ignore layer 0, the default stuff.
layer 0 is represented by the first bit, so you don't need to shift anything:
int layer$$anonymous$$ask = ~1;
or if you want to think of it this way (shifting 0 just does nothing):
int layer$$anonymous$$ask = ~(1 << 0);