If the animation starts, call function

Guys I have a problem:
I have a player and enemy, The enemy followes the player and when the enemy touches the player, The enemy’s animator “Paunch” will be called.
I have a health bar that i want if the enemy touches AND the enemy’s animator “Paunch” calls then minus 20 hp to the player.
I did the health bar script and all but in my game, when the enemy touches the player, The player loses 20 hp, i dont want that i want that if the animator “Pancuh” will be calles just then minus 20 hp to the player, any ideas guys?
thank you anyway <3

Hey!

You could just create a bool so that when your animation is played, it is set to true. Then, you could have an if statement in your Update function checking to see if the bool is true and then doing -20 to the player and setting the bool to false:

    {
    Paunch.Play();
    paunchBool = true;
    }
    void Update()
    {
    if (paunchBool)
{
health -= 20f;
paunchBool = false;
}
    }

Hope this helps :slight_smile:

You can easily make it by creating an event on your animation.

Let’s say your character’s health is an int and its variable is called health

1- Go to your character script and create a funcion:

public void Paunch()
{
health -= 20
}

2- On your animation choose the sprite you want your character to loose health, for exemple the first sprite in the Pauch animation

3- Click on the timeline of this sprite

4- On the Animation Screen you will see a button to create an event, its near of the Play animation button, click in the event button and an event will be created on the timeline at the point you selected.

5- Now click on the created event and choose to activate the function Paunch() you created at the script.