- Home /
How can I check if an animation is playing or has finished? (Using Animator/C#)
I'm using the animator component to animate my character in my game and C# to code it, and I want the game to know if an animation is playing or has finished, because they are a lot of things I intend to code into the game that would require knowing if an animation is playing of finished.
I've looked it up, and the only solutions I've found were in Javascript and for the Animation component.
If it helps, I'll put my situation in, my character pulls out a gun, I want the game to know that while it does that, the player cannot shoot, reload or change weapons until that animation is finished, how would I do that?
I'm talking about the "Animator" component, not "Animation".
Answer by thor_tillas · Dec 12, 2012 at 10:58 AM
If you use the animator and mecanim, then I think you should do something like that :
if (this.animator.GetCurrentAnimatorStateInfo(0).IsName("YourAnimationName"))
{
// Avoid any reload.
}
Be aware that your animation name should be similar to "Layer_Name"."State_Name" but if you use a "sub-state machine" in mechanim then the animation name must be "Sub-StateMachine_Name"."State_Name".
I hope it may help you!
Cheers, Flo
Is there one for when the animation has finished? Or even because I've now come across a situation where I would need to check that.
Also, What does the number of the "GetCurrentAnimationStateInfo" represent?
You could use AnimationState.normlizedTime to figure out the play count (decimal part) and the current progress (floating part).
2.25f would tell you that the animation has played two times and the current state is 25% (225%). Using modulo with 1.0f gives you always the progress.
From what I understand, you would need to check this condition every frame; is there way that is more optimized, like an event that fires up when the animation finishes?
Answer by mrSaig · Aug 27, 2014 at 09:12 AM
I check it with the time ... after i switch to another animation (like with a trigger) i have following condition:
if (Animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 && !Animator.IsInTransition(0))
works like a charm for me
the check for animator is in transition check is mandatory cause otherwise there will be for 1 or 2 frames the time of the last state unity resets the time after the transition is completed
Nice capital letter at the beginning of your sentence.
Also, the above didn't work for me, but this did:
if (this.GetComponent().GetCurrentAnimatorStateInfo(0).length > this.GetComponent().GetCurrentAnimatorStateInfo(0).normalizedTime) { //Do stuff }
Answer by edu4hd0 · May 01, 2016 at 12:11 AM
To know if Animator is playing any animation:
bool AnimatorIsPlaying(){
return animator.GetCurrentAnimatorStateInfo(0).length >
animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
}
To know if is playing a specific animation:
bool AnimatorIsPlaying(string stateName){
return AnimatorIsPlaying() && animator.GetCurrentAnimatorStateInfo(0).IsName(stateName);
}
This was what I needed, too. But Just a heads up here- comparing length with normalizedTime isn't what it seems. Length is the total seconds in the animation and normalizedTime is a mix of loop number and percentage completion- see https://docs.unity3d.com/ScriptReference/AnimationState-normalizedTime.html
This will consider the animation no longer playing after it has completed one full animation:
bool AnimatorIsPlaying(){
return animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1;
}
Took me a bit to figure why $$anonymous$$e were lasting so long with edu4hd0's code.
The method shown to know if Animator is playing any animation does not work, it actually makes no sense (tested).
Answer by YasinJavaid_ · May 19, 2019 at 09:40 AM
@alok-kr-029 i just simply your solution.
just send your animation name in CurrentAnimation you want to check.
public IEnumerator CheckAnimationCompleted(string CurrentAnim, Action Oncomplete)
{
while (!Animator.GetCurrentAnimatorStateInfo(0).IsName(CurrentAnim))
yield return null;
if (Oncomplete != null)
Oncomplete();
}
calling coroutine
StartCoroutine(CheckAnimationCompleted("Shoot", () =>
{
Animator.SetBool("Shoot", false);
// Your any code
}
));
Answer by thor_tillas · Dec 13, 2012 at 02:28 PM
I cannot add some code in the comment space, so I have answered there... sorry...
There is no "StateEnded" or "TransitionEnded" event for now on the mecanim system... So the only way to do that is to pull the "CurrentAnimatorStateInfo" and check if you enter the state/leave it.
if(this.animator.GetCurrentAnimatorStateInfo(0).IsName("YourAnimationName"))
{
// Avoid any reload.
this.InMyState = true;
}
else if (this.InMyState)
{
this.InMyState = false;
// You have just leaved your state!
}
The number is the layer index. Basically, if you have not created a new layer in your mecanim window, you only have one layer at index 0.