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