Physics.Raycast checking wrong layermasks?

Trying to do a Raycast only on layers 11 12 with following code:

if (Physics.Raycast (t.position,new Vector3(t.position.x,t.position.y-2,t.position.z),out hit,mask)){						
	Debug.Log ("Linecast: "+hit.transform.gameObject.layer.ToString());
	if (Vector3.Distance (t.position,hit.point)<2.2f){
		collision=true;
	}
}

mask is a public layermask with layers 11 12 assigned in editor.

the Debug.Log() however gives me layers 1 8 instead of the two i want to use.
Does anyone have the slightest idea whats going on?

goes to buy a pair of glasses

Thanks.

Actually, you won’t need them, since that is not true. The documentation is a bit misleading in that case, but it’s definitely not the case that the Raycast is being done on all layers except those in the layer mask. It’s a bad term Unity used there.

However I found this interesting thread about your problem:
http://answers.unity3d.com/questions/175545/physics-raycast-layermask-problem.html

I haven’t tested this myself yet but I can remember than I ran into a similar issue a while ago.

So my suggestion is to first make sure you are sending in the right parameters in the right order and then also try to get the value from the layer mask and put that into the function (yourLayerMask.value).

edit: The layer mask value seems to be the most important part:
http://docs.unity3d.com/Documentation/ScriptReference/LayerMask.html

Since no one else has mentioned what I think is the most likely cause (I messed this part up myself when I first used raycast) I’ll throw it in. Your code doesn’t show the actual construction of the bitmask, but this can be a small gotcha, doing something like mask = 11 | 12
or even mask = LayerMask.NameToLayer("Layer 11 name") | LayerMask.NameToLayer("Layer 12 name")
are both incorrect, what you need to do is bitshift the number 1 by the layer number and combine the result: mask = (1<<11) | (1<<12)
This stems from the fact that the layer mask is interpreted as a binary number where each 1 digit in binary represents a layer you want to check (starting at layer 0). Which means that the decimal value of the number you want to submit in this case would be 6144 which translates to 1 1000 0000 0000 in binary, which is how the bitmask is actually read.

I always get confused with layermasks as I’m never sure if i should pass the layermask itself or layermask.value to things like raycast.

Glockenbeat: After inverting the layermask, strange enough it worked.

Ghost314: bitshifting was one of the first tripwires i stumbled over when starting with unity. I won’t make the mistake of using an unshifted int in a raycast again :wink:
But since you asked: first i tried with

mask = ((1<<11)|(1<<12));
and
mask = ((1<<12)|(1<<11));

just in case it makes a difference, later i just did public layermask mask; and set the layers in editor