- Home /
Colliding Melee Weapons
What would the setup look like (RigidBody, Collider, IsKinematic? Constraints? IsTrigger?) to have two melee weapons colliding?
I would like the translation/rotation of the weapon to be determined by the animation (hand), but if it collides with another weapon (shield) should stop or even add a force to the opposing weapon. Also if it hits the character's collider give out damage.
Answer by danivdwerf · Aug 18, 2016 at 10:20 AM
He, I don't know if you are still having troubles, but I fixed mine.
You can just put a collider on your weapon that follows your weapon. than you can check with a collision or trigger enter with the player(or enemy). If you do that, it will also do damage if you just walk by and accidentally walk to close to the collider.
put this as a variable
private int _playerAttackStateHash = Animator.StringToHash("Base Layer.Attack");
//Base layer is the default layer the animation will be played on, if you have multiple layers, you want to change it. the Attack is the name of your animation on the layer(name it excact the same).
now you can only do damage if: the weapon hits you AND he's is playing his attacking animation
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
//Checks if attack animation is playing
AnimatorStateInfo info = _animator.GetCurrentAnimatorStateInfo(0);
if (info.nameHash == _playerAttackStateHash)
{
//Do Damgae;
}
}
}
but what if your animation makes the weapon hit the player or enemy BEFORE it should do damage?? Then you can put animation events in your animations, so you can only do damage between certain frames of the animation.
put a script on the same object as your animator, and make a pulic function with one integer parameter. Also make a public int and in the function, set that value to the parameter.
click on the attacking gameobject with the animation on it. in the inspector, go to animations and open the tab events. on the frame you want put an event and at function you fill in your function name. In the int slot, you fill in one. put another event on the frame you want to stop dealing damage and do the same, but fill a 0 in this time. Now you can only do damage if: the weapon touches you AND the attack animation is playing AND if you are actually hitting in the animation.
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
//Checks if attack animation is playing
AnimatorStateInfo info = _animator.GetCurrentAnimatorStateInfo(0);
if (info.nameHash == _playerAttackStateHash)
{
//checks if the animation Event gave through the number I set up
if (ANIMATORSCRIPT.INTEGER == 1)
{
PlayerHealth.health.Hurt(30);
}
}
}
}
good luck with your ganme
Answer by Nevadaes · Jul 31, 2013 at 12:41 PM
You can attach a box collider or even a mesh collider directly to the weapon / shield GameObject, so that, whenever your hand animation moves it, the collider will move and rotate along. You can then add collision callbacks for those two type of objects only, so that you can manage weapon collisions.
Afterwards, the choice is yours: you can either trigger an animation upon collision, use kinematics so that animation continues but that the weapon looks effectively stopped, etc.
Thanks for the reply, but won't two Is$$anonymous$$inematics pass through one another?
A problem you will have then (and which I'm struggling with right now) is, the weapon does always damage if you hit it. you can check collison only when attacking, but it still may do damage when you don't want to