Why Would a Raycast Ignore a Layermask??

Why would a raycast ignore the set layerMask?

    void Update(){
        //
        if(dragging){   
              Ray ray = new Ray(transform.position, -Vector3.up);
              Vector3 dir = -Vector3.up * 10;
            RaycastHit hit;
            if(Physics.Raycast(ray.origin, dir, out hit, gridLayerMask)){   
                print("object hit, layer = " + hit.collider.gameObject.name);
                  ///ABOVE PRINTS DESPITE OBJECT RETURNERD NOT BEING IN LAYERMASK!!!!!!!
              }      
        }
    }

What is the layer number for the grid? And what is the corresponding gridLayerMask you are using?

The grid layer, “grid”, is layer number 9. The floor layer, which is on the object being returned is on layer 8, “floor”. “floor” is not included in the gridLayerMask on the single instance I have.

How do you create the gridLayerMask?

As a public LayerMask.

Edit. Also, when is place the offending object, floor, on floor layer, which is being called incorrectly… When I switch it to another layer, say Default, it is still called. Very odd.

What is the actual value of the grid layer mask?

If you post code, post every relevant bit. You are leaving out essential pieces of code for us to be able to understand what you are trying to do.

I am not sure what you mean. How can a layerMask have a value? The gridLayerMask, contains one active layer, “grid” or int value 9.

@Fluzing , I will post shortly. Away from desktop.
Cheers!

I am asking for gridLayerMask.value.

it should look like this:

public LayerMask layer_mask = 1 << 9;

@Dantus ,
Hm. I am not sure, can check later when on computer. In the meantime tho, I see in the docs it converts a layerMask value to an int. why is this relevant here?

As far as I know, setting like this is no different from setting the value in the inspector. No?

No.

You need to add .value in the parameter.

Fluzing is right. I battled this for almost a full day about a year ago. You have to use .value.

Everyone is wrong
As usual, read the docs.

public static bool Raycast(Vector3 origin, Vector3 direction, RaycastHit hitInfo, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers);

You are passing a layermask as the distance parameter
Just put in a distance param and it will work. You dont need to use mask.value, it’s implicitly an int

Also why are you using dir when ray already has a direction; and dir being 10 units long is pointless, because Vector3(0,-1,0) and Vector3(0,-10,0) function the same

1 Like

It’s a bit value for the masks. 1 << 9 is not 9, it’s the binary number 0001 0000 0000, which is 256. You can set a collision mask either as 256 or as mask |= 1 << 9;

Bitwise stuff. Good stuff.

Thanks, that makes sense. However, while it did stop calling a collider on a layer not part of mask, it is not calling object in layer mask. ??

I have double checked everything. The layerMask, is assigned to only receive “grid” layer. Object collider I am trying to detect has a collider and is on grid layer. My ray is casting in the proper direction, its distance is infinity, so I am unsure the issue now. Totally puzzled.

??

EDIT. Ok, so I ticked off the trigger of grid, and it receives. I thought a trigger could receive raycast hits. No?

It might help to tell us what you get from this:

Debug.Log (gridLayerMask.value);

The answer is (probably) here
Look carefully
http://docs.unity3d.com/Manual/class-PhysicsManager.html