What's better for fighting system?, Trigger or Raycast?

Hello Unity Community!

I’m making a 2D hack n’ slash videogame. I was making the fighting system with box colliders(2D) which are triggers. For example, I put a trigger in every enemies’s attack animation which it’s going to be able for 0.1 seconds. So everytime this animations plays it’s going to do damage to the player if player’s collider detect it. (By OnTriggerEnter2D function)

The problem is this system isn’t working at all. Sometimes doesn’t detect the collision so when enemy attacks 10 times the player only recieve 8-9.

I’m almost mewbie on this and I didn’t find a correct way to do it. Someone can help me? Which is the right way to make a good hit detection? It would be better if I use a raycasting system?


Did you try to extend the lifetime of the collider to confirm that the collision actually takes place?

Did you try putting the enemy collider on a different layer (one that only collides with the player) to be sure that the collision hits the player only?

I personally use collision detection and did not experience the issues you mentioned.

Side note:
I think the detection should be made on the enemy side not on the players side, this will allow you to keep logic where the action is rather than keep it all in the player

Also you could use animation events. Set an aevent in animation settings at specific time, and then choose a method that you want to run at this specific time. Could be a raycast for example. Or, if you dont need a point of impact, just calculate if it hit or not.
I did not test this for performance, so there could be faster methods.


With triggers, you should cache what players are currently in range (collision). This can be done easily by adding them to list/array OnTriggerEnter2D and OnTriggerExit2D. That way when player slashes, you go through the list and hit everything in it. I think triggers are a good way of doing this.

This would work only with static colliders, if you want to really detect when weapon has hit the enemy, you should set rigidBody2D collision detection to continous and it should work flawlessly, but it will cost a little more performance to count the collision. With this method you don’t need to cache anything, you just detect OnTriggerEnter2D on the player that has been hit.