Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Azound · Jan 31, 2010 at 05:59 AM · collisionphysics

How do I disable collision for an object

I know that I can disable rendering an object by using renderer.enabled = false, but how do I disable a collider? Surprisingly, there is no collider.enabled property, and I don't want set the entire gameObject as inactive. Does anyone know of a way to do this?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Brian-Kehrer · Jan 31, 2010 at 06:02 AM

Physics.IgnoreCollision will do it.

Alternatively, you can set Collider.isTrigger to true, which doesn't entirely eliminate effects, but may do what you want. Setting isTrigger also means you don't have to ignore collisions between your object and all the other objects. Toggling this is probably faster than removing / readding the component, but you might want to test that.

If you want to remove it more permanently, you can use: Destroy

EDIT If this is already a trigger, why not just set your own custom flag on an attached monobehaviour? You can filter the state yourself in much the same way, in OnTriggerEnter, etc, just call MyScript.IsColliderEnabled. It may involve a GetComponent call in OnTrigger events depending on your setup, but this is definitely better than destroying / creating colliders.

public class MyScript : MonoBehaviour{ public bool isColliderEnabled = true;

 public void OnTriggerEnter(Collider other){
     if(isColliderEnabled){
         //do stuff here
     }
 }

}

on the other collider you would just check if the collider had an attached component of type MyScript, and if so, check isColliderEnabled.

Also, consider creating a separate GameObject just for the collider and parenting it to your original object, so you can enable / disable it without enabling or disabling other components.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Azound · Jan 31, 2010 at 06:08 AM 0
Share

The object I'm using is already a trigger, so that won't help. Physics.IgnoreCollision unfortunately only disabled collision between two specific colliders - it doesn't disable a single collider completely. It seems like the way to do this might be to destroy the collider completely, and then create a new one later when I want to "re-enable" it? That seems a bit heavyweight, and I'm really surprised there's no enabled property on the collider.

avatar image StephanK · Jan 31, 2010 at 10:53 AM 0
Share

I guess the last paragraph (parenting) is what you need to do then. ;)

avatar image Azound · Feb 01, 2010 at 05:39 AM 0
Share

Thanks, I think that will do what I need :)

avatar image DragonReeper · Nov 01, 2010 at 04:15 AM 0
Share

I think it is easier to just swap the collider mesh with nothing but how?

avatar image psykojello · Dec 13, 2010 at 09:39 AM 0
Share

Doing the "isColliderEnabled" was the best solution for my problem (which seemed similar to the original posters)

Once I detect the trigger, I set isColliderEnabled to false so it doesn't get triggered again. Thanks :)

avatar image
3

Answer by replay11 · Feb 02, 2012 at 11:08 PM

You guys are going to LOVE this, I'm sure! LOL

Ok,

I tried using...

 collider.enabled = true;
 // and
 collider.enabled = false;

And although this is what the Unity Scripting reference tells you to do here... file:///Applications/Unity/Unity.app/Contents/Documentation/Documentation/ScriptReference/Collider-enabled.html

For whatever reason I could not get this to work in my iOS game build. It worked fine while testing inside the Unity Editor, but every time I did an iOS build of my game and tested it on my iPad2, it never worked. This was incredibly frustrating and discouraging, but I finally (after hours and hours of testing with no success) I just decided to give it a rest and sleep on it. That very night I had a dream about it and I know this is going to sound CRAZY, but, here it goes... Jesus spoke to me in my dream and told me the solution to the problem...which was to just animate the Radius property of the collider. Now I know your probably falling off your chair laughing right now, but hey, at least your not crying because you can't get your game to work! LOL ...AND what he instructed me to do in my dream actually really did work! See below...

The solution that actually worked for my game was ...

  1. Use Unity's built-in Animation system to animate the "Radius" property of the collider from it's normal size to really super tiny (shrink collider)

  2. Create another animation that animates that same "Radius" property on the collider, but this time animate it going in the exact opposite direction (from super tiny to normal size... grow collider)

  3. I then appropriately named the animations ... "collider shrink" and "collider grow"

  4. Then whenever I would normally want to use "collider.enabled = false; ", instead I just use "animation.play("collider shrink"); "or where I would normally be inclined to use "collider.enabled = true;" I now use " animation.play("collider grow");"

I hope this helps someone out there who is struggling (like I was) to get their game to work. And I hope it put a smile on your face! :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image amirabiri · Jun 09, 2012 at 05:39 PM 0
Share

LOL I wonder why your subconsciousness thinks it's Jesus...? ;-)

avatar image
0

Answer by petrucio · Apr 05, 2012 at 05:55 AM

 void Start() {
     mBoxSize = (collider as BoxCollider).size;
 }
 
 public void Enable (bool isEnabled) {
     (collider as BoxCollider).size = isEnabled ? mBoxSize : Vector3.zero;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by GWJustin · Jul 13, 2012 at 06:55 AM

Here's what I'm doing. I have a class boolean variable with a getter and setter. To turn collision off I search through the gameObject for all colliders and set them accordingly:

 internal var _isColliderEnabled:boolean = true;
 
 
 function get isColliderEnabled():boolean
 {
     return _isColliderEnabled;
 }
 
 function set isColliderEnabled(value:boolean):void
 {
 
  _isColliderEnabled = value;
     
  //get all colliders in the object
  var colliders:Component[] = this.gameObject.GetComponentsInChildren(Collider);
  //print(this+" isColliderEnabled="+isColliderEnabled+" colliders="+colliders+" "+colliders.Length);
     
  var collider:Collider;
  for(var i:int = 0;i < colliders.length; i++)
  {
  collider = colliders[i].collider;
  collider.isTrigger = !value;
  //print(i+") "+collider.gameObject);
  }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Make Ray hit own collider 1 Answer

How to make physics collision dotted simulation like bubble shooter ? 2 Answers

Arcade Physics and colliders 3 Answers

Disable inertia tensor calculations on rigidbody? 0 Answers

Unity Collider Jerkiness/Teleportation (2019.2.17) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges