OnTriggerEnter2D problem, help !

There is 2 cubes, and both have Box collider 2D and rigidbody.


when one hit the other, why OnTriggerEnter2D is called so many times?? what’s wrong??

void OnTriggerEnter2D(Collider2D other)
{
Debug.Log(“HIT”);
}

And my last project seems to have the same problem, somehow i fixed it (not through coding)… and now I’m stuck again. help pls.


I have to ask, why do you want to register a collision while your actual GameObjects are not even close to touching? Reduce your collision boxes to fit your image and try. Or am I missing something?

Same, still not working.
And for you first question, the reason i’m doing that, is i need to use onTriggerEnter to pass on values.

For example, i need to know where the head at ,and what it points to.

Ok, fair enough, just wanted to make sure that was intentional. I’m assuming both objects have a Rigidbody, check the Collision Detection pulldown, make it discrete. Also maybe you should check what ‘Object’ is in your debug call. You will probably identify the problem if you know what is colliding with what.

I’ve tried.

void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("HIT: " + other.name);
}

just don’t know why the function is not called one time but many times. And here’s my last project.

It works fine. I don’t know how i figured it out.

Well that’s great you have it working, not so much that you do know what you did to solve. Rigid body physics don’t allow one object to intersect with another. If it is forced, the two objects will move themselves to solve the impossibility of two solid objects occupying the same space. On the other hand, colliders are just sensors to trigger a collision, as long as your collider is colliding it’s going to fire, that’s what it’s supposed to do. Maybe try two colliders, one box you already have and maybe an edge collider at the upper position you need. While you will still get a collision but you can use to set a flag and ignore the remaining collisions on that collider. You can also test the collision by hand by testing the distance between your object and its destination, if it is less than a fixed amount trigger whatever you were doing in the collision routine.

I’m real new here so maybe there is better advise to be found with the experts.

No~~ I’m still struggling!! The working one is my last project… Maybe i should use OnTriggerExit2D… but it means i need to change everything. I guess i have to now lol. Anyway, thanks for your help, appreciate it.

What version of unity are you using? I recall that there was one that had collider issues.

Otherwise there must be something wrong with your code, as OnTriggerEnter2D would only be called once. How do you move the objects?

Version is 5.3.3, i know it must be something wrong, and not coding, because it’s very simple just debug.log. I just move the objects left or right.

Or could you upload your testing project, it could be very helpful. It must be something wrong in some components in objects.

here you go…import into a 2d project + open the included scene.

PS: i updated to 5.3.4 though…it works as expected

2557816–178026–TriggerTest.unitypackage (5.43 KB)

1 Like

Oh my god, thanks to you, i found out why!! it’s because i set ‘Gravity Scale’ to 0… That’s why!!!

Say you want to make a top-down 2D racing game. How would you test if car passes the finish line if gravity has to be 0?

No what i suspect is that you are moving the object wrong. Try changing rigidbody’s position, instead of Transform’s position.

That said, in my test app i can’t make OnTriggerEnter2D not work. Gravity can be 0 and i tried moving it both ways. It logged all hits.