It seems that the 2D collision system will not generate an OnTriggerExit2D if the local scale of an object changes while the collision is happening…
I have to exit the collision, re-enter it and THEN an OnTriggerExit2D event will happen.
Are you aware of that bug and is the a work around or do I need to do something special?
Thanks!
When the local scale is changed during the collision, is the collider still also resized with it? Is it still within the bounds of a collision? I used the following script on a gameobject and the only side effect was that when the object was scaled down it generated another OnTriggerEnter event but still generated the Exit as well.
using UnityEngine;
using System.Collections;
public class TriggerTest : MonoBehaviour {
private bool scaled = false;
void OnCollisionEnter2D(Collision2D col) {
Debug.Log("COLLISION ENTER");
}
void OnCollisionExit2D(Collision2D col) {
Debug.Log("COLLISION EXIT");
}
void OnTriggerEnter2D(Collider2D col) {
Debug.Log("TRIGGER ENTER");
if (!scaled) {
transform.localScale = new Vector2(0.7f, 0.7f);
scaled = true;
}
}
void OnTriggerExit2D(Collider2D col) {
Debug.Log("TRIGGER EXIT");
}
}
Well, I update the local scale of object 1 in my Update loop when Object 2 is in collision with it. So that works and physics collisions are happening because the object 1 scaling is pushing all objects around it. Object 2 stays at the center of object 1 and doesn’t move until I move it away.
But I get not OnTriggerExit event
Hmm, I can’t really say anything else without seeing what is happening or looking at how it’s being done via code. Please share something on here that can help the community diagnose your problem. The issue honestly sounds like a matter of implementation and not a bug.
My trigger is following the mouse, so I can do rapid movements and exit the collider very quickly. I don’t know if your test can do that.
I have removed everything and I can see that if I scale in the OnTriggerStay2D loop or in Update or FixedUpdate, or LateUpdate, the OnTriggerExit2D Debug.Log I have there will not display every now and then. It is missing the event.
If I do NOT scale and move the trigger in and out quickly, I get 100% Exit messages…
There is something going on here…
Can you make a test and tell me if you get the same with a trigger linked to your mouse?
Cheers.
By changing the transform you are essentially warping the collider through space which, for the same reason you shouldn’t directly position a rigid-body, causes contacts to be regenerated. Any existing contacts will just be replaced by new ones at the new position.
Unfortunately, Box2D does not provide the ability to perform arbitrary transformations (position, rotation or scaling) on colliders resulting in the need to regenerate them internally. This causes Box2D to destroy any existing contacts on the collider (potentially many internal fixtures in the case of a polygon collider) and regenerate new ones.
This doesn’t work well for how we expose these as completely mutable component inside Unity and essentially it causes new contacts to be produced.
We are looking at how we can at least correlate contacts prior to a rescale/transformation-change with new incoming contacts caused by the internal regeneration.
Thanks a lot MelvMay!
That explains it. I didn’t have that problem with Chipmunk physics, as I could call a Chipmunk.Update when I changed the transform, without a loss in performance.
So I’m back to Chipmunk. I hope you guys can solve this issue.
Thanks again for taking the time to look at this.
I am currently working on my Ludum Dare 28 project and experience the same problem.
My weapon should determine with OnTriggerEnter2D/Exit2D if an enemy is in range. It´s attached to my player object and he is scaled by using an Vector3 and the localScale value.
Any news about this? Has someone found a solution (that works in my setup) to this problem with basic Unity Box2D physics? (no third party engines likle chipmunk)
I got the same problem
I desactivated the 2d box collider in animation, or I tried to move it by moving it center in animation, but it doesn’t fire OnTriggerExit2D…
In these conditions 2D colliders are useless… hope it will be fixed soon
I have implemented the contact correlation for 2D physics and it has been released to an internal alpha build.
This essentially deals with the fact that Box2D re-generates contact information and looks at the surfaced collision/trigger state and ensures that you continue to get “Stay” conditions even if the contact was regenerated due to (say) the collider being moved or the internal Box2D fixture being recreated.
Previously you’d not see the “Stay” callback but instead just see a new “Enter” callback. Also, the algorithm will ensure that if you were in a “Enter” or “Stay” callback and due to a move/regeneration you’d then get the “Exit” callback.
In short; what you see on the Unity end for callbacks will work as expected no matter what we’re forced to do because of the way Box2D works.
Any estimate of when this fix might be available? My game objects might not always be symmetrical so re-scaling them but not their colliders like suggested isn’t a very scalable fix for me.
Unfortunately, it’s because there still hasn’t been an update. This is half a year after the 2D system came out, and yet still no bug fix update. I’m very disappointed in my experience with Unity due to this. Other popular Engines try to make a case of at least an update a month, which is more than reasonable. I hope the Unity team reworks their update pipeline to be more inline with their increased popularity… Pushing all these bug fixes into an incredibly later release that requires it to wait for a bunch of new features they are working on, is quite silly.
Or at the very least, make Beta’s public, letting people choose if they want to risk any other new bugs.
If they’ve got a fix in the works for ANY upcoming version, that’s still way better than their record on Unity’s horribly broken joystick support. It’s been 5 years and counting on that issue for me.
My advice: Don’t wait for them to fix things. Find a way to work around the problems or you may end up waiting forever.