Search Unity

OnCollisionEnter2D not working

Discussion in '2D' started by PJRM, Dec 28, 2013.

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

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    Hi.

    It is only me or
    Code (csharp):
    1. OnCollisionEnter2D(Collision2D other) { }
    is not working!?

    - I have Rigidbody2D attached
    - There are a Circle Collider 2D attached with "IsTrigger" = false

    I have 2 objects with the same properties. WhenI touch each other, OnCollisionEnter2D don't work.

    Ps.: OnTriggerEnter2D is useless to me. I need contacts points.
     
    LezzGoLuc and WillieP like this.
  2. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    It works just fine. Post some screenshots of your inspector for the object you are debugging and I will try to help.
     
  3. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    Sure. here they are:
    $Laser Inspector.jpg $Rock Inspector.jpg

    The Rock Code:
    Code (csharp):
    1.   void OnCollisionEnter2D(Collision2D other) {
    2.     Debug.Log("Test");
    3.     if (other.gameObject.tag == "Boundary")
    4.       return;
    5.  
    6.     if (Array.FindIndex<string>(Properties.Tags_Damaging, x => x == other.gameObject.tag) >= 0) {
    7.       // the other object can be hit.
    8.       Destroy(other.gameObject);
    9.     }
    10.  
    11.     if (Properties.Explosion_Prefab)
    12.       Instantiate(Properties.Explosion_Prefab, other.contacts[0].point, Quaternion.identity);
    13.   }
    the Laser Code:
    Code (csharp):
    1. void OnCollisionEnter2D(Collision2D other) {
    2.     Debug.Log("test");
    3.     if (other.gameObject.tag == "Boundary")
    4.       return;
    5.  
    6.     if (other.gameObject.tag != Tag_Owner) {
    7.       // particle effects
    8.       if (Properties.Explosion_Prefab)
    9.         Instantiate(Properties.Explosion_Prefab, transform.position, Quaternion.identity);
    10.  
    11.       // destroy
    12.       Destroy(gameObject);
    13.     }
    14.   }
    None print "Test" on the Console
     
  4. TaewYn

    TaewYn

    Joined:
    Jan 30, 2013
    Posts:
    19
    Try turning off isKinematic. For some reason isKinematic also disables collisions and triggers when used in 2D. It's been causing me trouble too, as there currently is no easy way to trigger between two objects that are kinematic.
     
  5. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    +1, very annoying but it's true. If you don't want physics affecting your gameobjects set gravity scale to 0.
     
    Elia5five, sandersonphil and WillieP like this.
  6. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    Well...
    Code (csharp):
    1. Gravity Scale = 0
    2. isKinematic = false
    but for some reason, my objects are being moved without any code to do it or gravity to affect them. Well... one problem goes, another comes... hehehe. aahh :sad:

    #Edit#
    My Boundary which check the limits! him is the problem. I'm using a Cube covering all the playable area (without mesh, only collider), so that way i can use Parallax bounds. When a object touch the limit, it is transported to the otherside (exemple: reach the right limit, you get to the left limit). I check this using OnCollisionExit.

    If there is an object inside other collider, it will be moving. I'll have to change my Parallax limits idea.

    Thanks you guys so much!!!!
     
    Last edited: Dec 29, 2013
    disturb160 likes this.
  7. 3dgrinder

    3dgrinder

    Joined:
    Oct 21, 2008
    Posts:
    249
    Also if you change scale property of 2D sprite then collision get messed up and physics start feeling jerky. I heard there is a problem with box2d and Unity transform property change but not sure.
     
  8. Sammael

    Sammael

    Joined:
    Dec 11, 2012
    Posts:
    24
    Probably it's too late... But if you check the box/option "Use Full Kinematics Contacts" in the inspector it may work, it actually worked for me
     
    preislter and Baccho_4h like this.
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    You're replying to a post from 2013! Note the option you're referring to didn't exist back then.
     
    samooniyakk and Siccaz like this.
  10. wildBcat

    wildBcat

    Joined:
    Apr 1, 2020
    Posts:
    3
    I know your answer was not correct for the OP, since they didn't have that option back then, but this solved a big headache for me yesterday. Thank you!
     
    Betty_Bytes, samooniyakk and MelvMay like this.
  11. AaronEkstrand

    AaronEkstrand

    Joined:
    Nov 26, 2017
    Posts:
    1
    To wayward travelers searching bugs on Google, check out Physics2D.IsTouchingLayers.
     
    jgarbeidi likes this.
  12. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    333
    Things like this can really be frustrating, being that upgrading one of 2D games made to a higher version of Unity, I guess you would expect things to break, but after 1.5 hrs of debugging why my 2D collisions just don't work, I simply tick the box "Use Full Kinematics Contacts" and it works like it should.

    I should refer to the manual much more often, and sooner rather than 1.5hrs later:
    -Larry
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    I'm not sure I follow. Are you saying that this option was on for a certain Rigidbody2D prior to upgrading then after it was turned off? I don't see how that can happen TBH; this would be the first time I've seen it reported. This option isn't a "fix" for anything so I'm confused over your comment.
     
  14. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    333
    @MelvMay I can't tell you whether this tick box was enabled or not prior to upgrading, I have to assume it was, as my collision detection was working with no issues, and then just stopped working after upgrading. As for your quote "This option isn't a "fix" for anything", I guess if you say so, but it did fix my issue. Could you elaborate beyond the documentation what it is for?

    -Larry
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Normally Kinematic vs other Kinematic/Static body-types don't produce contacts (only vs Dynami). When it's on the system calculates them for you meaning the Kinematic in question produces contacts like a Dynamic body-type but still acts like a Kinematic body i.e. no geavity, forces or collision response.

    Again though, a contact doesn't mean a collision response though. For instance, triggers produce contacts but there's no collision response. What you will get with this option are callbacks and the ability to use GetContacts.
     
  16. gwelkind

    gwelkind

    Joined:
    Sep 16, 2015
    Posts:
    66
    To any other n00bs like me, if you're using kinematic mode you probably want `OnTriggerEnter2D` instead.

    Or switch to Dynamic if you want to use OnCollisionEnter2D

    "Collision" is explicitly meant for things bonking into each other, which you probably don't want if you're not making something with physicality that responds to forces :)
     
    O_DEMON_O likes this.
  17. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    The body type (Static, Kinematic or Dynamic) has nothing to do with the Trigger/Collision callbacks doesn't change them. If a collider is set as a trigger then you'll get "trigger" callbacks but if not you'll get "collision" callbacks.
     
  18. unity_71AEA40C6739CC7AE9D1

    unity_71AEA40C6739CC7AE9D1

    Joined:
    Nov 9, 2021
    Posts:
    1
    Hi! I tried almost every solution provided in this thread and still no results. I’ll attach the trigger code below and some screenshots of my inspector options. Oh and btw I've started Unity development just a week ago so I'm a pretty big noob.

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D other){
    2.         if (other.gameObject.tag == "Player"){
    3.             Debug.Log("TOUCHED");
    4.  
    5.             Destroy(this.gameObject);
    6.         }
    7.     }
    (this was the enemy code because I want him to destroy itself when touching the player)

    Player:
    upload_2021-11-12_12-20-55.png
    Enemy:
    upload_2021-11-12_12-26-38.png

    Very grateful to anyone that can help me!
    Thanks!
     
  19. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Please, don't hijack existing threads with what you assume is the same issue, just create your own thread; it's free and easy. :) Often these threads get filled with lots of posts, all different but the title sounds the same so they act like a problem magnet.

    Also, you don't state how you know it's not calling that trigger. This happens SO many times. No console message because it's buried inside other logic. It's not testing if the callback happens, it's testing if the callback happens AND other logic is working. Put the debug log as the first thing if that's what you're testing for or learn how to attached a debugger which is an essential skill moving forward.

    Then there's the "Do you have them set to contact in the Layer Collision Matrix"? This is all stuff I presume you've covered in a basic 2D physics tutorial?

    Nothing in your script above shows an obvious problem apart from what I've said above although it's not clear why you've set all the constraints on. A Kinematic body will only move how you tell it to; it'll not react to collisions. No need to constrain rotation if you don't ask for rotation. No need to constraint X or Y if you don't ask it to move in X or Y.
     
  20. sodonikagames

    sodonikagames

    Joined:
    Nov 19, 2021
    Posts:
    4
    I tried every solution but none of them worked. I have a solution.

    Project settings > Time > Fixed Timestep

    It is 0.02 by default change to lower numbers like 0.001.
     
  21. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Time to lock this thread. If you have an issue, please create your own thread.
     
Thread Status:
Not open for further replies.