- Home /
Ignore/Destroy then re-enable Mesh Colliders
Is there a way to destroy a mesh collider when lets say the user clicks "m". And then create the mesh collider again when the user clicks "m" again? Or instead of destroying and recreating ignoring then un-ignoring? I have an object that I have rendering and un-rendering when I click "m". But when unrendered it's still interactive.
Answer by zachypin · Jul 07, 2011 at 04:32 PM
Well I ended up not being able to figure out the IgnoreCollision thing though that would've been nice. I didn't want to do the destroy -> addcomponent because that's just too expensive. Ended up just moving the object lightyears away then moving it back. That way I won't be colliding with it and it'll look like it went invisible. I guess it's the cheapest way to do it, though it seems like a band-aid to me, which I don't like :(
So this is a way to solve the problem. If anyone checks this out and can figure out how to correctly do IgnoreCollision using the mouse and an object as the two colliders do let me know. Thanks!
I made a short edit to my answer below. Not sure if it'll help or not, but there it is : )
awesome! I'll definitely have a look next week and hopefully delete this answer :)
Answer by PrimeDerektive · Jun 30, 2011 at 08:45 PM
You'd have to destroy it and then recreate it with AddComponent, you can't disable a collider component.
You could maybe do something with Physics.IgnoreCollision(), or looping through the collision layer matrix and make the disabled objects' layer ignore everything else; of the three, I'm not sure what would be the most performance-effective.
But using the IgnoreCollision I can't find anything that would allow me to "un"-IgnoreCollision
Take a look at the reference docs for it. There's a third parameter that IgnoreCollision
takes and it's a bool
. Just toggle that.
okay cool. Only thing I'm wondering now is how to reference the mouse as one of the colliders. (this.collider, collider, true) doesn't work because the first two things reference the same thing. I want to say something like, (mouse, collider, true)
I'm confused; when did the mouse come into this? If you're looking for a solution on pointing to an object on screen and enabling/disabling it on clicking, use what you've learned here and combine it with some raycasting. There is a plethora of questions here on similar topics so take a look through them.
If you're then having problems with raycasting, open a new question on that topic.
If @Derek-Traver 's answer fixed things for you, don't forget to mark it as the accepted answer : )
Didn't :( The reason the mouse comes into play is because I'm using the On$$anonymous$$ouseUp() function. That function is using the mesh collider to interact with. I'm not sure what to set the two colliders up as not being interactable. I'd like to just do IgnoreCollision(true). but you need 3 things in there. (colliderObjectOne, colliderObjectTwo, true).
Answer by Chris D · Jun 30, 2011 at 08:51 PM
A similar question has been asked a few times before. Please remember to search for your questions before opening a new one.
The most pertinent example I could find is this one. For many components in Unity you can just set object.isActive = false
but apparently that doesn't work with colliders. Instead, if you're already disabling the renderer, you can just tell the mesh collider to ignore collisions with Physics.IgnoreCollision
or by turning the collider into a trigger (and not doing anything with OnTriggerEnter).
Better solution edit:
So I took another look around and apparently there's a function called `Physics.IgnoreLayerCollision` that works much like Physics.IgnoreCollision
except it deals with entire layers of objects. If you placed your mesh on a separate layer, when you disable the renderer, you could also just tell whatever collision you're using to ignore the layer the mesh is on. There's a short feature page on this thing here.
As to your mouse thing - you say you're using OnMouseUp: so are you then raycasting? If you're ray casting, there aren't two objects to use (like what it sounds like you're saying above...) but you can still alter the collision settings.
I know the question's marked as answered now, but I thought I'd put this out here too, just in case.