- Home /
Is there an isPlaying() type function for Animator
I know you can do animation.isPlaying() but is there something similiar for Animator ? So if I have:
Animator animator;
void Start()
{
animator = GetComponenet<Animator>();
}
Then I could go:
void Update()
{
if(Input.GetMouseButtonDown(0))
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
if(!animator.isPlaying("ShootAnimation"))
{
animator.SetTrigger("ShootAnimation"); // play shooting animation
}
}
}
Comment
So the answer is no then, right? Unless I have overlooked something.
Best Answer
Answer by looMeenin · May 04, 2014 at 01:09 AM
I ended up doing this and it worked:
if(!animator.GetCurrentAnimatorStateInfo(0).IsName("ShootAnimation"))
{
animator.SetTrigger("Shoot");
}
Answer by edu4hd0 · Apr 30, 2016 at 06:06 PM
This works for me:
bool AnimatorIsPlaying(){
return animator.GetCurrentAnimatorStateInfo(0).length >
animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
}
Be careful. Normalized time is, as the name says, normalized. That means, it reaches the value 1 when the animation is finished, no matter the length. So you should not check against length, but against 1. Note that this will not work as intended for looped animations, where normalizedTime will become larger than 1.