Is Movement Required for OnTriggerEnter2D to fire?

Would appreciate if someone knowledgable could help me with the question in the title, first post here!

I am currently using Trigger colliders for Hitboxes in a game with some combat mechanics, and am noticing when a punch is thrown (Colllider components enabled) involving two stationary colliders (one non-moving trigger hitbox for the punch, one non-moving collider hurtbox with dynamic rigidbody), OnTriggerEnter is not fired.

If something bumps into either of these combatants while this is happening, OnTriggerEnter will then be fired.

~

I found that you can hack this a bit in the editor btw by disabling the Trigger component, skipping a frame, and then re-enabling it on the next frame. This seems to work, but trying this in a StateMachineBehavior script does not.

~

Curious what the understood approach may be to implementing Hitboxes correctly for sprite sheet animations.

I am going to set up a new scene and experiment with what I have using AnimationEvents → Physics casting (CircleCast) for hitbox detection, then try using Kinematic rigidbodies + non-trigger colliders as well.

No, movement isn’t required. When two colliders overlap they produce a contact. If either of the colliders is a trigger then the contact is a trigger contact. Those get reported via OnTriggerEnter2D, Stay2D and Exit2D.

I don’t follow your description here though, both are described as non-moving so how can they come into contact? Maybe show what they are in an image or something and how you’re moving them.

Correction: Trigger collider contacting dynamic rigidbody with a trigger collider.

Second correction: I am observing the same behavior even when both are stationary. (first contact: OnTriggerEnter fires; subsequent: it does not). I am wondering if there is a project setting that might be responsible for this? The following example involves both GameObjects being stationary.

I am setting a GameObject with a collider trigger to active to represent a punch (see ‘Chomp’ GameObject in image).

This is firing correctly, as noted by the HP of Seed(Clone) decrementing to 80 HP from 100. Indeed, the Chomp hitbox will be noted in OnTriggerExit when I set its GameObject to inactive, concluding the ‘punch’. However, every subsequent punch is not being reported.

In case it is relevant, I am on version 2021.28.f1.

Sorry but you have a really clear understanding of what’s going on but you’re communicating it in a really confusing way.

So on the left side of that sentence it’s a Static (non-moving) Trigger Collider? Presumably the CircleCollider2D you show in the image.

What behaviour? If both are stationary, how can their contact state change? It’s like you’re asking for Enter, Enter, Enter, Enter etc.

Hold on, so one of these Collider GameObject is being deactivated/activated? When?

If you stay in contact, you’ll only get an OnTriggerEnter2D once. From then on you get OnTriggerStay2D then finally OnTriggerExit2D. If you disable then that state is lost and you’ll get OnTriggerEnter2D if overlapped and active.

Maybe post some script unless you can clearly state the flow of what’s going on here. Alternately, just create a new project and do a simple test then explain that.

Correct.

The hitbox collider in the photo is not moving, the plant with dynamic rigidbody in the photo is not moving. So I am setting the hitbox collider’s gameobject to active to represent a ‘punch’, and am deactivating it when I want the ‘punch’ to end.

Thank you Melv, I’ll whip up a sample scene as a demonstration.

Whip up a sample project and host it and I’ll take a look. Delete the Library/Obj folders to keep it small though. :slight_smile: