I'm developing a melee combat system and I'm wondering if anyone has any suggestions or best practices for how to determine hits and blocks.
Is the use of colliders attached to the hands and feet of my animation the best approach or should I look at a raycast instead and then just measure the distance between the two gameobjects to determine a hit?
What are the advantages disadvantages to each approach?
Is there another way that I'm not considering?
I can explain the method I'm using, but I'm not sure it's a good one (works for me so far).
I get the vector between the player and the target, then the dot product with the vector player_center, player_hand). Finally, I cast a ray along the first vector (player, target) with the dot result as distance max. It might be more clear with a picture :
Advantages : The damages are applied when the bone is close enough of the enemy, so it match visually. And the animation doesn't need to know about the size of the target.
Disadvantages : The animation have to be created properly, or it wont reach the enemy.
[ ** EDIT ** ]
If you are still interested, I just figured an approach wayyyyyyyyy simpler and more efficient. You just need to know at what % of your attack animation occures the hit, like 50%. Then you calcule the animation progression, and if it's higher than 50%, it's a hit (once). Take a look at the code above.
var div : int = animation["attack"].normalizedTime + 0;
var progression = (animation["attack"].wrapMode == WrapMode.Loop) ? animation[anim].normalizedTime - div : animation["attack"].normalizedTime;
if( progression < 0.3 ) didHit = false; // Make sure we hit just once
if( progression > 0.5 && !didHit ) Hit();
I’m using a very easy and visualisable system. You can create an empty transform in front of the player and make it a trigger. You can change its height and width to suit your range. Then reference the box using a public variable from your script. Finally use OnTriggerEnter to check if the enemy is in range. Then you can check if the hit input key is pressed and apply the necessary changes to the enemy.