- Home /
How to check in code when an animation is over?
I have a 2d game where, if the the character is idle for a few seconds, he switches to an impatient state where he checks his watch, and then goes back to idle. The code I have so far is this
if (grounded && !moving)
{
CurrentState = StateIdle;
counter += Time.deltaTime;
if (counter > 15.0f)
{
CurrentState = StateImpatient;
}
}
This works, but the problem is that the character won't go back to the idle state because I haven't specified when it should do that. I think I should just check in code when the impatient animation is done playing, and then have it automatically play the idle animation again. Assuming this is the correct way, would anyone know how I can go about doing that? Thanks in advance.
Answer by houseyokeswaran · Jan 22, 2018 at 09:19 PM
You can set up a callback function in the animation window. Select your gameobject with the animator component in the hierarchy and select your impatient animation. Move the vertical line to the last frame of animation and add a new event. You should be able to select your public function from your script a dropdown in the inspector window.
image for reference: https://i.imgur.com/Jfx5sOl.png