2D Trigger only once? (SOLVED)

I am making a 2D knights vs goblins type of game. Each characters attack is dealt by a trigger box that is activated when they are attacking. My problem is there are going to be swarms of enemies in the game, and when lets say the player swings his sword, it knocks every one back because the trigger is activated in all of them. What I am trying to get to happen is have the player/weapons trigger only deal damage to one enemy at a time, so the player would only knock back one out of a certain number of enemies, not the whole group. Please note that all the enemies have the same tags, and layers. Also, I am using OnTriggerEnter2D in the enemies controller to know when to take damage and knockback. Any way I can do this easily and efficiently? Thanks!
Please note that I want this to happen for arrows from archers as well, so finding the nearest target from the player would not work.

Add an integer to your trigger script that is increased by one on each
OnTriggerEnter2D event.
Add also an if statement that checks if a certain amount has been reached. If not cause damage.
Reset the integer each attack

2 Likes

Thank you, I will try this and will comment if it works!

Didnt work, the code is correct as it works when the enemies are a little whiles away, but when they are right inside eachother they still get damage in groups because the code is happening too fast. Any tips?

EDIT: Nevermind, It turns out I had the knockback in another script section so it looked like they were still taking damage. Works perfectly, thanks for the help!!!

1 Like

I’m glad it helped.