Trouble ignoring layers with physics.Linecast()

I have a ball and fire a linecast down to check if the bal is grounded and what angle the floor is. I want the LineCast to ignore any trigger volumes it might encounter.

So I set trigger volumes to layer 10, named layer 10 into IgnoreLineCast and use the following code.

var hit : RaycastHit;
var targetPos = transform.position;
targetPos.y -= 1;

if (Physics.Linecast (transform.position, targetPos, hit, 10))
{
    Debug.DrawLine (transform.position, hit.point, Color.red);
}

But nothing happens, the debug line is not drawing. When I change to

if (Physics.Linecast (transform.position, targetPos, hit)) 

it works fine, the floor below is not marked as layer 10 so I do not get whats wrong here.

You're not understanding the last argument, the layer mask. See the docs.

A quick fix could be

...
var allButTriggers = ~(1 << 10);
if (Physics.Linecast (transform.position, targetPos, hit, allButTriggers))
...

To clarify a little, 1 << 10 means to shift a number 10 bits toward the most significant bits, so the layermask 10 is expressed. Then we use the ones complement operator on this result (~) that will cause every bit that is not set to be set, and those that are set to not be set. In effect, this invert the bits.

  • Note that it might be cleaner to use a public variable (and more flexible) for the LayerMask. You can then change this easily in the inspector.

    `var linecastLayers : LayerMask = -1;`

(Here, -1 means all layers which is the default value in this scenario.)