- Home /
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?
Answer by AdamAz · Mar 05, 2015 at 11:18 AM
Hi
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
It works extending the lifetime of the collider but I tried the same yesterday and didn't works because I didn't extend enough.
Now another problem arise, when I attack at the same time the enemy hits me twice in one attack(It's like the collider collides with two object). And I don' know why because the enemy's hit collider it's in a layer that only collides with the player, and the player's hit collider it's in a different layer that only collides with enemy. What's wrong so?
These are the layers: Player, Enemy, EnemyHit and PlayerHit. EnemyHit only collides with Player, and PlayerHit with Enemy.
Thank you for your help!
$$anonymous$$aybe there is a second collision which you do not see? Try disabling the enemy attack collider right after the collision was detected, something like:
void OnCollision2D()
{
this.attackCollider.enabled = false
... // rest of your logic
}
Answer by Woj_Gabel_FertileSky · Mar 05, 2015 at 10:44 AM
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.
Answer by adsamcik · Mar 05, 2015 at 10:33 AM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613185112im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Collision detection accuracy issues. 1 Answer
Why does this script check for raycast collision? Is it necessary? 1 Answer
Can't click gameobject when over another trigger? 1 Answer
Trigger-based vehicle ai? 1 Answer
Trigger Spawning? 1 Answer