checking sprite collision once even if the sprite is already inside the collision box

I’m making an area type battler where enemies come up next to you, and you can click to attack them with your sword. Only OnCollisionEnter won’t work all the time because sometimes when you attack, the enemy is already within the collision box. Also, OnCollisionStay will get triggered multiple times. I thought about possibly making a boolean to flag once something has been hit once, but that might be a bit messy if there are multiple attacks with different timings and such.

Is there an easier way to trigger a collision once regardless of whether the collision is already inside the box or not without additional manual work?

Use OnCollisionEnter2D + OnCollisionStay2D. Use a list to track everything you’ve hit during each attack.

There are probably more elegant ways to do this depending on how you have thing structured but that should work well enough.

1 Like

As was suggested above, keep track of all the enemies you have hit. In case of multiple attacks / combos just set them up so that each attack increases a counter and use that to disable an enemy from getting hit by each attack multiple times.

“Hit X enemy with my 0 attack, if my attack is now 1 it can get hit again” and etc.

I’m sure there’s better ways, though. Try searching YouTube for battle/sword mechanics.

1 Like