- Home /
Recommended approach for melee combat system
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?
Well this is certainly an interesting question! I think I can just give you some advice more than I can an answer. If you are going to work with exact collisions of who hits where when for parts of the rigidbody. You will need know this: http://unity3d.com/support/documentation/Components/class-Rigidbody.html, maybe you can work something out with parenting and joints ins$$anonymous$$d of superprecise high performance cost calculations
Answer by Berenger · Dec 07, 2010 at 08:45 AM
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 : http://yfrog.com/2psanstitreuhkj
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();
How do you set up your character to easily measure the player_center -> player_hand vector?
I need a handle for the hand bone in my script. Two methods here, either with a public var and a drag&drop in the editor, or a script attached to the bone that answer to a "Broadcast$$anonymous$$essage( WhereAreYouHandBone )" with a "Send$$anonymous$$essageUpwards( ThereIAm, this )", something like that. I'm starting to figure out a problem with that approach though, it's possible i'll need to do something different for long range attack (not sure how that's called in english, attack with a bow etc).
Answer by game_dev23 · May 09, 2019 at 01:03 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090059im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Safe area from enemies 1 Answer
OnTriggerEnter - destroy (this.gameobject) if it collides with anything 2 Answers
Looking for help with differentiating collisions between game objects. 2 Answers
How do I get a Not collision? 5 Answers