Best way to make melee AI damage player?

Hello Knights of unity Answers.

I have stumbled upon this kind of annoying problem with my AI. Everything is up and running fine, he chases the player until close enough to hit, then stands still and hits him, and if the player goes out of range again, he chases him again.

The only thing is, i can’t figure out how to make him actually affect the ‘health’ variable on my player each time he plays the ‘attack’ animation, which is just about once every second. How would i go about with this?

Thanks in advance! Answers much appreciated.

Well you should put your damage scripts to make it easier, but you can write a function that causes damage to the player if it hits its collider, and place it in the object that has the animator component. Call it CauseDamage() for example. Then go to the animation clip that should do the damage and add an event to the frame that you’d like to cause the damage, and as an event add that function.

Type in google unity3d health bar and you will get tutorial.