How to access specific animation state as animation

This isn’t so complex as it sounds. I have an animation Component and I want to wait till an animation that is playing to stop.
This is my code:

function WaitForAnimation ( animation : Animation)
{
    yield; while ( animation.isPlaying ) yield;
}

...
...
hit.collider.gameObject.animation.PlayQueued(animationToPlay);

WaitForAnimation(hit.collider.gameObject.animation[animationToPlay]);

The thing is that hit.collider.gameObject.animation[animationToPlay] is of type AnimationState and while I need to wait for the specific animation to stop playing. There are 5 or 6 animations attached to my gameObject and I don’t really care which one is currently playing, I just need to wait for the current playing animation to stop.

I tried with simply putting WaitForAnimation(hit.collider.gameObject.animation); but the function exits immediately, without really waiting for the animation to finish.

What I am trying to do is the following:

I have 4 separate animations. Each one of these rotates the object 0->90 or 90->180 or 180->270 or 270->360 (that’s why 4 different animations).

When the object for some reason needs to be animated from 0->90 and then immediately from 90->180, the 0->90 animation doesn’t manage to complete to its time. So a hiccup is being observed from x (where x is between 0 and 90, and represents the 1st animation’s angle when the 2nd animation started being rotated) to 90, which is the 2nd animation’s starting angle.

So, that’s why I used PlayQueued() so as to wait for each animation to finish before the next to start.

But, I have problems with the accuracy of it. For example, if I play the 1st 3 animations simultaneously (with PlayQueued()) and wait for all to finish, I observe angles of 271,1234 and such. That’s why I wanted to have WaitForFinish, so as to manually fix the angles of the objects.

But, if there is a way to fix the PlayQueued accuracy issue, it would be preferred.

Try printing weight and enabled properties of hit.collider.gameObject.animation[animationToPlay] in WaitForAnimation and see what’s happening. Maybe animation start is delayed by one frame (although it looks like your code should work fine in that case).

I’m not sure if there’s a better way to do it but you could try something like:

bool animationActiveBool = false;

void Update()
{

//animation condition occurs then
animation.Play ("Recoil");
animationActiveBool = true;

if (animationActiveBool == true)
{
if (!animation.IsPlaying("Recoil")
{
animationActiveBool = false;
}
}

if (animationActiveBool == false)
{
//logic for next animation sequence
}

}

That’s an example of the way I might handle this kind of problem, but you might need multiple bools for different animation states and then it’s just a matter of setting up the flow of logic so everything works nicely.

I’m not sure if that will help though it sounded like you were looking for a simpler alternative, I’m not sure if one exists.