Projectile causing onTriggerEnter to occur multiple times.

So the projectile I’m talking about has a Rigid Body and a BoxCollider2D added to it.

The enemy it’s hitting has around 24 Polygon Colliders on it. Only 1 of these colliders is active each frame. The reason these Colliders have been added is because I want to get a true representation of where the Collider should be as the enemy moves/animates. This wouldn’t be an issue if Unity let you animate a Polygon Collider on each frame - I would have just needed 1 Polygon Collider then.

So when the projectile is fired, it ends up entering several of the Polygon Colliders - this is why it’s triggering the lifecycle method - OnTriggerEnter - multiple times.

The only way I’ve been able to combat this issue is by sticking another collider on the enemy, a Cylinder Collider, which covers most of the enemy (not all of him, and it covers over sections of white space that aren’t the enemy) and then in the OnTriggerEnter I use an typeof to check if the Collider is a type of Cylinder, then damage the enemy.

Anyone know of a better solution here that would in fact use the perfectly mapped Polygon Collider, but only trigger the damage once per hit?

I’ve seen this sort of question asked before on the interwebs, but there doesn’t seem to be an ideal solution for it.

I think I would just have a certain amount of damage that the projectile can deal.

When it hits something and deals the damage, it gets subtracted from the projectile’s “store of damage.”

Subsequent hits would find that at zero and do nothing.

When the damage drops to zero, the projectile could kill itself off.

Another way might be to have all PC2Ds on the item register with some other script saying “I’ve been hit by projectile with instance ID X this frame, so ignore all other calls this frame.”

Would it be possible to create many smaller colliders that move along with the animation? Ie in 3d i would try to have a collider for hand, lower arm, upper arm and so on which is tied to the respective bones. Something like this is possible for 2d if the character‘s animations are made using a ragdoll. Of course this changes the style of animations more towards a puppet on strings in a 2d app.

Just for clarification, the projectile has a damage property on in so like float damage = 5.

I like the idea, so hit first collider, now the damage of the projectile = 0, so subsequent calls to OnTriggerEnter take 0 damage off the enemy. However, the projectile should be designed to travel through enemies and hit other enemies, so unfortunately this approach won’t work. Unless the projectile would get it’s +5 damage back when it hits a new enemy - so only become 0 on that particular enemy, then when it travels to the next it repeats (damage 5, damage 0 > next enemy damage 5, damage 0, and so on until the projectile moves off camera), is this what you meant? Wouldn’t be sure how to do this though.

The second approach may work, I’ll have to look into how to code that though :slight_smile:

Thank you for answering :slight_smile:

Yeah I like the idea, it’s just with Polygon Colliders, it maps around the sprite perfectly, instead of using shapes to get a “the enemy is sort of here” approach, as I’m not doing a skeleton rig style of animation, my sprites are frame by frame animation. I didn’t want to throw a projectile and it hit the sprite but then it not actually hit the collider. It seems strange how I can’t just update a polygon colliders on each frame when hitting record to animate to move the collider’s position around the sprite to marry up with the frame :frowning:

Thank you for answering :slight_smile: