- Home /
OnTriggerEnter2D fires multiple times when Animator component is on
Hi all,
I have an issue with OnTriggerEnter2D firing multiple times when a target's animator component is turned on. I've attached a project here to demonstrate the problem.
I created 2 entities, one called Car and one called Person, with the following settings:
The scene starts out with the Car to the left of the Person. The Car moves toward the person using the Car's rigidbody2D's velocity. If the Person's Animator component is on, the OnTriggerEnter2D in the Car script will fire what looks to be every frame their colliders intersect. When I turn the Animator component off, it behaves expected and OnTriggerEnter2D only fires once. It doesn't matter if anything is even physically moving - I created an empty state for the Person's controller and set it as default and it still triggers multiple OnTriggerEnter2D's.
I set up this scenario with regular 3D colliders and it behaves as I expected, so I'm leaning toward this being a bug with the new 2D tools, but I'd like to be sure.
I've already tried searching for OnTriggerEnter2D issues on Answers and on Google, but most of them either involve triggers not working, or assumed to be firing multiple times because of a symptom arising from bad code.
Any help appreciated, thanks very much!
[2]: /storage/temp/25081-carsettings.png
Answer by $$anonymous$$ · Apr 15, 2014 at 07:31 PM
I believe this indeed is a Unity bug. bansalgirish8's workaround only works in that specific use case, and even so, only once.
I've implemented a workaround where I maintain a list of colliders that have triggered OnTriggerEnter2D once before, and ignoring them for every subsequent trigger until they are removed by OnTriggerExit2D. Not sure it's the way to go, but it's the best workaround I've been able to come up with so far.
Answer by bansalgirish8 · Apr 12, 2014 at 07:43 AM
I faced the same problem.I have solved the problem in your scene attached below.
Answer by tabby_nat · Apr 21, 2014 at 07:32 AM
Seems like there are a lot of bugs with OnTriggerEnter2D. I have the same issue, where a transform.rotate causes OnTriggerEnter2D to fire on the same frame as the rotation, even though the trigger has not yet exited the collider. If your animation has a rotation of any sort, that would seem to reflect the same symptoms as my bug.
I'm pretty sure it's a bug.
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D - am I getting it wrong? 2 Answers
When you disable a gameobject, what happens to the rigidbody inside? 1 Answer
Keep Quad on Single Axis 0 Answers
set collision force after isKinematic disabled 0 Answers
[SOLVED] How to Destroy a rigidbody game object on collision with another gameobject? 4 Answers