- Home /
OnTriggerEnter2D fires multiple times
I have game in Unity 4.3 2D and I encounter this problem.
I have one object with box collider 2d, with a trigger and rigidbody 2d.
Another object with box collider 2d, with a trigger. I have attached a simple script to detect OnTriggerEnter2D to this object.
void OnTriggerEnter2D(Collider2D col)
{
print("OnTriggerEnter2D " + Time.time);
}
When I moved the 2nd object I expected trigger to fire once when it collide with 1st object. But it keeps on firing as long as I move the 2nd object while it is collided with the 1st object. Almost like OnTriggerStay ...
I am completely puzzled by this behavior. Is this normal for 2D or there is something wrong? I have attached simple project to explain what is happening.
I noticed if I moved the rigidbody object it works as expected, but if i move the trigger it behave this way...
I'm interested in this question. I have a project where it used a 3d rigidbody, but now I translated everything to physics2d and this part is the only thing that behaves completely different from my previous implementation.
Answer by clunk47 · Nov 25, 2013 at 04:47 AM
The project attached has 2 scripts: GameController and Bullet, neither have any OnTriggerEnter2D function... Also, the Bullet prefab has a child plane with a mesh collider. 2D Physics require 2D colliders, so the spheres need CircleCollider2D, not SphereCollider. The plane child of the bullet prefab needs the OnTriggerEnter2D function attached, and needs a 2D collider component as well. Trigger requires one of the objects to be a non kinematic rigidbody, I'd do this on the bullet, since it's moving. I've got it working on my end, I've attached the fixed project, hope it works well on your end.link text
EDIT: Wrong project was attached to original question initially, keeping information above for general information, working solution for specific question below:
It's because you don't have a Rigidbody2D component attached to the bomb. I attached that component to the bomb and copied the parachute's rigibody2D component values to the bomb's and presto. I just decided to make sure each object had the same components and they didn't, this seems to work, hope it works for you as well.
bomb object is colliding with the parachute. there are only 2 objects in the test scene. If I moved the rigid body object things work as expected. When I moved the trigger object it work this way ....
clunk47 thank you for answering my question. After looking at the project you uploaded I realized why no one was answering my question. I accidentally attached wrong project to the question. :(
This project I have, parachute, rigid body 2D, trigger, box collider 2D
bomb, box collider 2D
if you move parachute you get 1 OnTriggerEnter2D message
if you move bomb you get many OnTriggerEnter2D messages, while it is moving and colliding with parachute. This is the behavior I don't understand
It's because you don't have a Rigidbody2D component attached to the bomb. I attached that component to the bomb and copied the parachute's rigibody2D component values to the bomb's and presto. I just decided to make sure each object had the same components and they didn't, this seems to work, hope it works for you as well.
Yes. In 3D it only requires one of the objects, but in 2D it seems as if the object itself with the OnTriggerEnter2D requires a Rigidbody2D component as well. I think they should have done this in Unity3D in the first place, makes more sense to me lol. This could very well be a bug, since the 2D tools are new, but this is the only solution I could get to work while troubleshooting.
Yeah, I believe triggers require non-kinematic rigidbodies. Although if I remember correctly, sometimes kinematic works for me, sometimes not lol. There are quite a few undocumented details like this, so I'm assu$$anonymous$$g they are bugs. Anyway I appreciate the original question, it gave me an opportunity to check out the new 2D features in Unity :D
Your answer
Follow this Question
Related Questions
On Trigger Enter, Collide with object, specific collision 1 Answer
Trigger Spawning? 1 Answer
Colliding two GameObjects 1 Answer
Problem with OnTriggerExit 1 Answer