Disable OnMouseDown() for a Collider

Is there anyway of disabling the mouse press event for a collider?

I have two colliders, the first is catching mouse press, the other is used for something completely different. However, the first one is bigger than the second, so OnMouseDown() is never triggered in the smaller of the colliders, because the other one is on top. I know I can fix this by putting the mouse press collider on top in the z-axis, but it would be nicer to just disable mouse events for the first one.

Put the collider you want to ignore on the IgnoreRaycast layer.

–Eric

13 Likes

Thanks that worked. However, is it possible to tell unity to ignore raycast for a custom layer? I played around with the new layer matrix in unity 3, but couldn’t make this work.

1 Like

The Raycast function optionally takes a LayerMask parameter that you can use to selectively ignore objects on particular layers.

OK, but if I understand correctly, then I would have to implement the whole mouse click thing myself without using the build in feature that captures mouse clicks in OnMouseDown() ?

1 Like

Camera has an [eventmask ](http:// Unity - Scripting API: Camera.eventMask)for this.

4 Likes

This helped me a lot thank you :slight_smile:

I’ll add that unlike the culling mask, it seems that the eventMask can’t be set directly from the Camera editor, so i had to make it accessible via script.

Hi, you can do this…

[SerializeField] private LayerMask inputLayerMask;

private void Start()
{
Camera.main.eventMask = inputLayerMask;
}

Then in the game object’s inspector set what layer mask, or masks, you want to receive mouse input.

5 Likes