Search Unity

Ignore Collisions by Tag SOLVED!

Discussion in 'Scripting' started by Revenantue, Sep 15, 2010.

Thread Status:
Not open for further replies.
  1. Revenantue

    Revenantue

    Joined:
    Sep 15, 2010
    Posts:
    4
    I have been struggling with this for a while and I know a lot of people out there have too so now that I found a way to do it I thought I would share.

    Well, the problem is simple: how can I avoid collisions between objects with a certain tag?

    I tried collision managers, arrays of the objects that added themselves upon creation, etc. After a long journey I think I came up with a pretty simple and, sorry if I say so myself, elegant solution.

    Imagine that you create 100 instances of an object with a tag "Robot" and you want them to collide with everything except themselves. Well, the only thing you have to add to the script attached to the robots is this:

    Code (csharp):
    1. function OnCollisionEnter (collision : Collision) {
    2.  
    3.     if (collision.gameObject.tag == "Robot") {
    4.     Physics.IgnoreCollision(collision.collider, collider);
    5.         }
    6.  
    7. }
    And that's it!

    The instant the robot touches another one it will add it's collider to the list of ignored collisions. Actually, this happens so fast that it won't even get to bounce.

    This may be a primitive solution compared to the complex collision managers I've seen out there but I hope this helps someone. It's working perfectly for me.

    Cheers!
     
  2. LoTekK

    LoTekK

    Joined:
    Jul 19, 2010
    Posts:
    136
    Yeah, this works well enough in most cases. I used pretty much this exact method to get a S*** ton of rapid-fire projectiles to not collide with each other.

    Of course, Unity 3 finally introduces layer-based collision management. :)
     
  3. jwatte

    jwatte

    Joined:
    Sep 14, 2009
    Posts:
    6
    I wants. Right now!!!

    'cause I have the same problem. I'm making a basically 2D shoot-em-up, but with 3D art. Things have DamageDealer and DamageTaker components. I want things to collide if and only if (objectA.DamageDealer.friendly != objectB.DamageTaker.friendly).

    With most physics engines, you can assign mask bits and detect bits, such that you can easily set this up, but apparently not in Unity 2.6 :-(

    Any other bright ideas would be much appreciated!
     
  4. Nemox

    Nemox

    Joined:
    Feb 16, 2010
    Posts:
    396
    Wow, I completely forgot about tags! This gives me a great idea for a system I wanted to make...
     
    ComradeDom likes this.
  5. Fourthings

    Fourthings

    Joined:
    Feb 15, 2010
    Posts:
    313
    Nice work dude, cheers!
     
    ComradeDom likes this.
  6. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    Please don't dig out 18 month old threads ^^
     
  7. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    huh? You would rather have him create a new one?
     
  8. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    There was no point in posting anything. He didn't asked a new question or some clarification, basically just a "thanks" post, which bumped up an old thread and had no "added value".
     
    VagabondOfHell likes this.
  9. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    A 'thank you' always adds value to a thread.
    If it doesn't matter to who-ever is thanked, it matters to those who arrive later (when gauging whether this is sound advice).
     
  10. diablo

    diablo

    Joined:
    Jan 3, 2011
    Posts:
    736
    And so the Battle Royale begins! Stay tuned for some riveting Germany vs Denmark action!

    Update : Ok, looks like there's no further action on this front, fans are booing and lining up for their refunds.

    Morale of the story : If you want to thank someone for an old post, thank them via personal message; it will avoid international incidents such as this one!
     
    Last edited: Apr 17, 2012
    kill3r3ight, aCake0202, Ermiq and 3 others like this.
  11. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    The morale of the story is to let people express their opinions of the answers.
     
    ComradeDom likes this.
  12. Coned_Stoding_games

    Coned_Stoding_games

    Joined:
    Feb 21, 2014
    Posts:
    1
  13. crydrk

    crydrk

    Joined:
    Feb 10, 2012
    Posts:
    74
    I'm in need of doing this for many objects but 2D. I'm finding a couple different answers but this one is interesting. Is it considered good practice to do this ignore right as it collides?
     
  14. _DS_1

    _DS_1

    Joined:
    Sep 6, 2016
    Posts:
    17
    Hey man!! thanks for the info!! a six years ago... :)
     
  15. dgplaygame

    dgplaygame

    Joined:
    Jul 17, 2015
    Posts:
    1
    thanks you.you helped a lot
     
  16. Prixo_

    Prixo_

    Joined:
    Apr 9, 2016
    Posts:
    1
    2018 !!!!! ;)
     
    ComradeDom, ownerfate and Ignacii like this.
  17. HoneyTbone

    HoneyTbone

    Joined:
    Feb 13, 2019
    Posts:
    1
    2019!!!!!
     
    ComradeDom and NotaNaN like this.
  18. Studio22

    Studio22

    Joined:
    Dec 9, 2018
    Posts:
    3
    2019 thank you
     
    ComradeDom and BostonGamesLLC like this.
  19. jgree151

    jgree151

    Joined:
    Apr 16, 2019
    Posts:
    1
    Thanks to the OP this was super helpful
     
    ComradeDom and BostonGamesLLC like this.
  20. RegahProd23

    RegahProd23

    Joined:
    Jul 22, 2019
    Posts:
    5
    2019, Thank you!
     
    ComradeDom likes this.
  21. ethanicus

    ethanicus

    Joined:
    Aug 24, 2015
    Posts:
    40
    This doesn't seem to work for me in 2019.2. My projectile collides first, then disables collisions. It won't hit any other colliders, but the first one stops it dead in its tracks.
     
  22. waruban

    waruban

    Joined:
    Oct 18, 2019
    Posts:
    2
    waiting for 2020
     
  23. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    377
    Thanks, needed something simple for one collider, collision layering was too fancy. :cool:
     
  24. VincentJenei

    VincentJenei

    Joined:
    Aug 27, 2017
    Posts:
    1
  25. JupiterSky

    JupiterSky

    Joined:
    Jul 7, 2017
    Posts:
    20
    It's now 2020, gotta love this thread, ten years old.
     
  26. jan_b

    jan_b

    Joined:
    Apr 14, 2013
    Posts:
    1
    One more thanks from 2020!

    Also, for others coming here from the future, it may be noted that disabling collisions using layers is really easy now.
    Just make a layer for your type of objects (e.g. Projectile) and uncheck the Projectile-Projectile cell in the Layer Collision Matrix in the Project Settings > Physics panel.

    See https://docs.unity3d.com/Manual/LayerBasedCollision.html.
     
  27. mlgoreodog22

    mlgoreodog22

    Joined:
    May 4, 2019
    Posts:
    1
    Why is this thread still going now lmfao
     
  28. OfficialCoatsee

    OfficialCoatsee

    Joined:
    Mar 4, 2014
    Posts:
    3
    Helped me lol too. Another bump I guess. But hey these bumps help google decide on whether or not it's still relevant, and well, it is.
     
    viridisamor and jesusluvsyooh like this.
  29. Cytochrome

    Cytochrome

    Joined:
    Jul 12, 2020
    Posts:
    1
    2020, thank you
     
  30. suryadwar25

    suryadwar25

    Joined:
    Oct 31, 2019
    Posts:
    3
  31. NathoSteveo

    NathoSteveo

    Joined:
    Dec 7, 2019
    Posts:
    7
    Almost 2021!
     
  32. joanpescador

    joanpescador

    Joined:
    Dec 21, 2016
    Posts:
    122
    It seems elegant to me too.
     
  33. Judgeking

    Judgeking

    Joined:
    Apr 15, 2017
    Posts:
    17
    I agree man. What a waste... :)
     
  34. aCake0202

    aCake0202

    Joined:
    Mar 11, 2021
    Posts:
    26
    thank you!
     
  35. mMomarMm

    mMomarMm

    Joined:
    Feb 25, 2021
    Posts:
    1
    Thank you very much
     
  36. darkonixro

    darkonixro

    Joined:
    Apr 12, 2020
    Posts:
    1
    thank you
     
  37. Filthyfil

    Filthyfil

    Joined:
    Jul 17, 2021
    Posts:
    1
    2021!
     
    Kotorman likes this.
Thread Status:
Not open for further replies.