Search Unity

Editing Transform During OnTriggerEnter2D Short circuits others?

Discussion in '2D' started by bundy, Oct 21, 2014.

  1. bundy

    bundy

    Joined:
    Apr 14, 2013
    Posts:
    8
    Hey guys, I just wanted some verification on if editing the transform during an OnTriggerEnter2D event will short circuit the propagation of the event any further. As an example:

    Parent GameObject
    Rigidbody2D
    Script 1 (with OnTriggerEnter2D)

    Child GameObject
    Collider2D (Trigger)
    Script 2 (with separate OnTriggerEnter2D)​

    Target GameObject
    Collider2D (Trigger)​

    Let's say Parent and Target collide. On collision, if I edit the transform in Script 1 of the Parent in OnTriggerEnter2D, the event seems to stop propagating down to the child into Script 2. I've tested this with siblings as well and it seems to stop as soon as the transform is modified in anyway. I can see why this may be since we should be altering things through the Rigidbody2D methods and since we're technically altering the object composition mid collision detection. I just wanted to see if this was intended behavior and if I just missed a documented part of how the OnTrigger/Collision2D propagation works if you edit the transform.
     
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    That sounds normal. My rule of thumb these days is to do nothing in events except setting flags, then handle anything that needs to be done in Update and/or FixedUpdate based on those flags. Sidesteps a lot of potential issues.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    You should get a callback on both objects if all you are doing is modifying the transform however if you are deleting one of the objects involved in the collision then you won't unless you go into Physics2D settings (Edit/Project Settings/Physics 2D) and turn-off the "Delete Stops Callbacks". This was primarily designed so that you can control whether deleting an object involved in the collision pair will stop the callback in progress.

    As I say though, if all you're doing is modifying the transform then both objects will get the callback. You will also get a callback on the collider GameObject as well as the Rigidbody2D if it's on a parent GameObject.
     
  4. bundy

    bundy

    Joined:
    Apr 14, 2013
    Posts:
    8
    Hey Melv,

    Thanks for the reply! I setup an isolated project which wasn't deleting any components or GameObjects, yet it was still stopping the propagation of the collision. I just simply have an object colliding with one that has 2 OnTriggerEnter2D scripts in a parent/child hierarchy. It seems the child never receives the OnTriggerEnter2D event if I edit the transform's position, rotation, etc on the parent level. I'm running 4.6 b20 and can provide a link to a zipped up project demonstrating the issue if it's a potential bug.

    Un-ticking "Delete Stops Callbacks" did, however, make the collisions properly propagate after editing the parent's transform in the OnTriggerEnter2D event. Really strange stuff, but for now we've been getting around it by using Rigidbody2D methods such as AddForce, MoveRotation, etc.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Well technically that property (Delete Stops Callbacks) was designed for when objects were deleted but also has the side-effect of doing the same for when anything that can conceivably change the collision/trigger state so it's safe to use.

    It may be best to rename that property to something more generic.

    BTW: So you know, 4.6 is only kept up-to-date with 4.5 at minor releases so it won't have any of the patch fixes. I don't think anything in the patch releases affects your issue but I thought I'd mention it.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    > I'm running 4.6 b20 and can provide a link to a zipped up project demonstrating the issue if it's a potential bug.

    Yes please, submit that as a bug report. Give me the case number and I'll take a look.
     
  7. bundy

    bundy

    Joined:
    Apr 14, 2013
    Posts:
    8
    Thanks for the help Melv, I just submitted the issue along with a test project. The case number is:

    641928
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Thanks, I've got the case in front of me now. Looking...
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    I checked your case and yes, it's simply that you are modifying the contact during the callback which causes the callbacks to be stopped but as mentioned previously, this can be controlled via the 'deleteStopsCallbacks'. It's unfortunate that this was named explicitly as dealing with deletions because it does/should also cover any transform change to the rigid-body and/or collider.

    The best way to deal with this right now is to rename the 'deleteStopsCallbacks' to 'changeStopsCallbacks' as well as defaulting it to off. The 'deleteStopsCallbacks' can be made deprecated (warning) but still work via in the API for now.

    So in short, use the 'deleteStopsCallbacks' and it will continue to work after this change.
     
  10. bundy

    bundy

    Joined:
    Apr 14, 2013
    Posts:
    8
    Thanks for the prompt replies and for clearing things up! I'll take your suggestions and apply them into my project.
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    No problem at all. I'm pushing the change described above to 4.5.5p3 (not released yet).