onTriggerEnter only fires once if player doesn't move

So I’m building an 2D metroidvania-like action game.


I have a player, with:


  • rigidbody2d
  • a polygon collider, non-trigger
  • a box collider, is trigger, enabled = false, this one is the attack collider

And when I hit attack key, I set enabled to true for like 0.5 seconds, and then set it back to false.

I kept an eye on the check box besides the collider, and I’m sure this set enabled true and false is working properly.


Then I create another target object, with:


  • a polygon collider, non-trigger

When the target object has onTriggerEnter2D fires, I log it.


So ideally I will make some damage on the target when the onTriggerEnter2D fires.


However, if I let the player stand still and hit the target multiple times, onTriggerEnter2D will fire only once, I have to move my player a little bit, and hit target, to fire the function again.


I also logged it when onTriggerExit2D fires, so I am sure it exits. it just doesn’t enter anymore.


Another thing is, if I attack fast and hit the target continuously, the function does fire as many times as I hit it.

but if i stop a while (like 1 second?), and I hit it again, function doesn’t fire. i still have to make the player walk a little bit to fire the function again.


this situation happens only when:

( all collider on the target object are non-trigger ) OR ( target has rigidbody2D attached )

If I remove rigidbody2D, and add one isTrigger collider on the target, this thing will not happen.


Anyone can help me with it? I have no idea what causes this.

Thanks!

Good day.

I think you are confused with OnTriggerEnter and OnTriggerStay. AS its name says, OnTriggerEnter, is executed when enters, so only once. OnTriggerStay, is executed every frame that the collider is inside.

GoodBye!