Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

OnCollisionEnter2D not getting called from RigidBody2D collision

Discussion in '2D' started by jamesar, May 30, 2014.

  1. jamesar

    jamesar

    Joined:
    May 21, 2013
    Posts:
    9
    Hey everyone,

    I've been having some pretty severe collision detection problems ever since upgrading to Unity 4.5. To prove my theory I even rolled back Unity to 4.3.4 where my collision detection and functions worked again without flaw. I'm hoping someone may have some insight as to what the problem might be or that there are at least other people experiencing the same problem. Here's a little more detail:

    I've created a 2d scene that uses 2d rigid bodies, and 2d colliders. I spawn 2d objects from the top of the scene and the goal is to catch these objects. All of my collision detection is done in my player class's OnCollisionEnter2D function. Basically, if the Collision2D.gameObject.tag is equal to a certain tag, I add/subtract/ do whatever appropriately. This works for some of my game objects but doesn't appear to work with objects that have particle systems attached to them, and eventually when the game gets hectic, very few objects actually register the collision with the player.

    Hopefully someone can help me with this problem, otherwise It looks like I will be continuing to use Unity 4.3.4 for the remainder of this project. Thanks for taking a look!
     
  2. brockemon

    brockemon

    Joined:
    Dec 21, 2012
    Posts:
    45

    Yes I believe this is happening to me also.
     
  3. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Are your colliders being modified mid-collision? E.g., are you Destroy'ing the GameObject, deactivating the collider, or some such? For example, when you catch an object, do you have an event on the caught object removing itself? In 4.3, you could do that mid-collision without problems, but it seems that in 4.5 removing the collider during a collision/trigger event call may inconsistently prevent other events from being called by the same collision. I had to move my collider removal from OnTriggerEnter2D into FixedUpdate (via a flag I was setting anyway) to make everything work properly.
     
  4. jamesar

    jamesar

    Joined:
    May 21, 2013
    Posts:
    9
    I do have it set so that the game objects get destroyed when they collide with the player. In the object's OnCollisionEnter2D, I check if the tag of the collision is the player or the ground and delete it appropriately. In my player class's OnCollisionEnter2D, I check the tag of the game object then increase the score or decease it appropriately. I will play with the OnCollisionEnter2D function of the falling objects and will try toggling a bool and destroying it from fixed update instead. Thank you so much for the response!
     
  5. jamesar

    jamesar

    Joined:
    May 21, 2013
    Posts:
    9
    Can confirm that this worked for me. Instead of destroying it in the OnCollisionEnter2D function, I am destroying the GameObject from the FixedUpdate method. Thanks again @Pyrian!! You saved me time and a great deal of frustrations.
     
  6. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    My pain is your gain. Glad I could help! That pretty much ruined my Tuesday. :cool:
     
  7. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Can you guys report this as a bug? I'm pretty sure it should be OK to perform this sort of thing in OnTriggerEnter - at least that's my understanding that Unity wanted to fix this behaviour but now may need to look again.

    Thanks :)
     
  8. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Last edited: May 30, 2014
  9. Dszordan

    Dszordan

    Joined:
    Jan 13, 2014
    Posts:
    7
    Just wanted to throw my voice in to here. I'm having the same problem:

    In 4.3 this used to work:

    I have a Ball GameObject with a Rigidbody2D and CircleCollider2d and a Box with a BoxCollider2d. On collision both would recognize that they had collided with each other. The Box would Destroy itself entirely, while the ball was repelled at an angle.

    In 4.5 I can't get this to work and I believe something to the effect that the Box is destroying itself before the collision get registered to the Ball. So now the Ball passes through the Box, the Box disappears but the Ball continues moving. I've tested not destroying the Box, and when the Box remains, the Ball recognizes that it has collided with it.
     
  10. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Rejected!
    "By design", eh? I would swallow that easier if it weren't so inconsistent. Sometimes you call one first, sometimes you call the other first. The only way to get consistent results is to simply never disable a collider during a collision event callback. How is that "by design"?
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,417
    This was a change in direct response to many bug reports that the collision system continues to push out callbacks when any of the rigid-body or colliders involved in the contact are disabled/destroyed.

    Seems that there's a different opinion here as to what should happen. Another way to solve this could be to provide a way to signal that you no longer wish to receive callbacks and revert the behaviour to continue to push callbacks unless this is requested.

    The inconsistency as you call it comes from Box2D. It has no preference of one object over the other and when two colliders come into contact we simply perform callbacks based upon their ID. There is no other property of the collider that gives one a calling priority over the other. We use ID to give consistency over the order.