x


Can I enable/disable a collider so it will/won't detect mouse clicks?

I have some 'billboard' planes I use as UI objects in my scene. They have mesh colliders on them so I can detect when user mouse-clicks on them. I need to hide/show these things. The problem is, even hidden, the colliders are intercepting mouse-clicks, such that an invisible one in front of a visible one will 'hide' the visible one, so the visible one does not detect mouse clicks (I hope that made sense).

Is there a way to 'disable' a mesh collider such that it won't intercept the click? It has no 'enabled' field. Note that the renderer is disabled, that alone was not enough to disable the collider.

Or is there a better way to do mouse-clicks?

more ▼

asked Aug 18 '10 at 11:41 PM

DaveA gravatar image

DaveA
26.8k 153 171 257

BTW: I tried setting the collider's 'isTrigger' but that didn't seem to help in this case.

Aug 19 '10 at 01:05 AM DaveA
(comments are locked)
10|3000 characters needed characters left

4 answers: sort newest

Well, this doesn't solve the actual problem (disabling collider/mouse), but it's actually a cool work-around which works very well for me, so I'll share it: When I need to disable (hide) my collider, I use the Update function to reduce the scale (over time) to zero, and when I show it, return scale to 1. This shrinks the colliders so they are out of the way, and has the added bonus of looking cool while it's doing it.

more ▼

answered Aug 20 '10 at 11:16 PM

DaveA gravatar image

DaveA
26.8k 153 171 257

Seems it's an oversight, wish they'd fix it, but the above seems to work. Could also move it out of sight or otherwise just hide things.

Oct 12 '10 at 12:48 AM DaveA

Yeah, I agree. This is a really good work around. Not much of a solution though. Unity needs to fix this.

Dec 01 '10 at 07:21 PM diabloroxx

just stumbled into a case where i ended up searching for a bug that was due to collider.enabled = true not working... but to use your method directly, you could just set the collider scale to Vector3.zero rather than having to use Update()?

Jan 24 '12 at 11:14 PM ina

Yes that should work. Also see layers answer below

Jan 30 '12 at 10:04 PM DaveA
(comments are locked)
10|3000 characters needed characters left

Like Markq mentioned the best way is to use layers. If you just want to enable / disable the OnMouseXXX events of a certain GO use this:

edit

// disable Raycasts
gameObject.layer = 2;

// enable Raycasts (set to default layer)
gameObject.layer = 0;

Layer 2 (start counting by 0) is the IgnoreRaycasts layer if your GO is on that layer it will be ignored by unity.

more ▼

answered Jan 15 '11 at 03:39 AM

Bunny83 gravatar image

Bunny83
46.8k 12 50 210

I thumbed up for posting info on which layer is by default the 'ignore raycast layer' - but there's a mistake in the answer, if you want to disable you actually want:

gameObject.layer |= 1<<1;

and similar change with enable raycasts. :) (1 << 1 : sets it to layer 2, the default raycast ignore layer, not sure why layer 4 is being used here)

Apr 26 at 11:16 PM SinisterRainbow

Yes, you're right, but it's not 1<<1 ;) My mistake was that the layer property on a gameobject holds the layer index and not a layer mask

The ignore-raycast-layer is layer 2 (but you start counting at 0 so it's the 3rd layer). The decimal value of the constant Physics.kIgnoreRaycastLayer is 4 == 1<<2, however we need the layer index instead of a layer mask. I will edit my answer ;)

Layer    Name           Index   LayerMaskValue
----------------------------------------------
Layer0   Default        0        1 == 1 << 0 == 00000001
Layer1   TransparentFX  1        2 == 1 << 1 == 00000010
Layer2   IgnoreRaycast  2        4 == 1 << 2 == 00000100
Layer3                  3        8 == 1 << 3 == 00001000
Layer4   Water          4       16 == 1 << 4 == 00010000
Apr 29 at 07:10 AM Bunny83

ic. layermask value of 2 (aka, 1<<1) incidentally still works.

Apr 30 at 01:19 AM SinisterRainbow

:D Yes it does work, but doesn't make any sense since the layer property is not a layermask. You just need the value 2 (the index) and not a bitmask representation.

Apr 30 at 07:44 AM Bunny83
(comments are locked)
10|3000 characters needed characters left

What you can do is to put the object on another layer by setting its layer property and then excluding that layer when raycasting. You perform the hit test as follows (the excluded objects are on layer 8 in this example):

RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),
    out hit, Mathf.Infinity, ~(1 << 8))) {
    print("It's a hit!");
}

See also: Layers in Unity documentation

more ▼

answered Dec 08 '10 at 08:53 AM

Markq gravatar image

Markq
36 2

If you use the built-in IgnoreRaycast layer, you don't even have to do anything else differently. Also works with OnMouseDown etc. (which use raycasting behind the scenes). If you use a custom layer(s), you may find it simpler to use the LayerMask type instead of bitshifting.

Dec 08 '10 at 09:37 AM Eric5h5

This sounds pretty good. My objects are already in a special layer (which I toggle to hide/show all these objects), so I should have thought of this myself earlier. I'd have to make an additional layer then. Eg: I have a 'data' layer which displays these 'data objects', I might have to add a 'hidden data layer' which would hold them while 'disabled'

Dec 08 '10 at 08:53 PM DaveA
(comments are locked)
10|3000 characters needed characters left

Yea, I don't think you can enable/disable colliders like some of the game objects other components. I could be wrong...but... you could "destroy" the collider when you don't want it used then "addcomponent" it when you want it...

I'm not sure if this is the best way, I'm still very new to Unity...

more ▼

answered Aug 19 '10 at 04:04 AM

joedrigon gravatar image

joedrigon
231 5 6 13

I think the overhead of create/destroy, and the iffy-ness of garbage collection precludes me using this, but thanks.

Oct 12 '10 at 12:47 AM DaveA
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1765
x1404
x1001
x232

asked: Aug 18 '10 at 11:41 PM

Seen: 5621 times

Last Updated: Apr 30 at 07:44 AM