Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OnCollisionEnter does not register

Discussion in 'Physics' started by anthonov, Aug 17, 2020.

  1. anthonov

    anthonov

    Joined:
    Sep 24, 2015
    Posts:
    160
    I have a rigidBody with one child and this child has a collider.
    I have a script that register collision on the child because it has a specific collision fonction for this collider.

    BUT the collision event does not register when colliding against another rigidBody.
    If I remove the parent's rigidBody the collision register.
    I dont want the collision script to be on the parent for reasons.

    So is this the normal behaviour for OnCollisionEnter() ?
     
  2. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I have the same problem.
    I'm not sure why that is, but I also find that surprising.

    I added a kinematic rigidbody to the child to fix it for now. But that means I cannot overlap the colliders of the structure or there will be a constant collision occuring.
     
    Last edited: Nov 5, 2020
  3. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    So weird, I've just tested it and it's working fine on my pc... With no offense... are you sure you only have one rigidbody at your GameObject hierarchy? Child might has another and thats why it's breaking, no sense if not.

    PD: Tested on 2020.1.0, forgot to update...
     
  4. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    @rubcc95
    Yes the structure is like this:
    Code (CSharp):
    1. Parent - rigidbody + collider
    2.           Child - collider + script (with OnColliderEnter() that does not get triggered)
     
  5. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Sincerely not idea what it's happening, but if you upload the prefab i can play with, thats my free day.
     
  6. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Alright, here it is: (I'm on Unity 2020.1.11)


    There's a scene with 2 balls rolling to the rigidbody structure and only the parent with the rigidbody will call OnCollisionEnter (not the child)
     

    Attached Files:

  7. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Well I got it. Seems like it works as you expect at 2D Environment, where i was trying cause i had a 2D project opened.

    But the "bug" seems to not be a bug. It works as intended, only rigidbody root is able too trigger the collision events... So you can't fire it though the children gameObject.
    But you can get the collider and the gameObject attached to using:
    Code (CSharp):
    1.  
    2.     private void OnCollisionStay(Collision collision)
    3.     {
    4.         Collider colliderWhichTriggered = collision.contacts[0].thisCollider;
    5.         GameObject childWhichTriggered = colliderWhichTriggered.gameObject;
    6.     }
    https://forum.unity.com/threads/oncollisionenter-does-not-work-on-a-child-colider.36462/

    EDIT: Thats your collider, no the other collider :) sry
     
    Last edited: Nov 5, 2020
    FeastSC2 likes this.