Animation Playing Late

Hello everyone,

I am working on a small game right now. I am currently using Legacy Animations since that is the only way the asset I bought works with animations. I am controlling only three animations with a script using an enum structure. Everything seems to be working okay besides one animation, which plays late. I feel that it may be due to the order of events being called, but after debugging all of the functions I am at a loss as to what is going wrong. I even tried moving the play animation code around with no luck.

Here is the animation control script which can be accessed from other scripts:

using UnityEngine;
using System.Collections;

public class HockeyPlayerController: MonoBehaviour
{
    private enum HPState
    {
        Idle,
        Shooting,
        Celebrating,
    }

    private HPState state = HPState.Idle;
    private Animation hpAnim;

    void Start()
    {
        hpAnim = GetComponent<Animation>();
        state = HPState.Idle;
    }


    void Update()
    {
        switch (state)
        {
            case HPState.Idle:
                // Check to see that other animations
                // are not playing before going to Idle
                if (!hpAnim.IsPlaying("SlowSkatePoseShoot") && !hpAnim.IsPlaying("Score"))
                {
                    hpAnim.Play("Idle");
                }
                break;

            case HPState.Shooting:
                hpAnim.Play("SlowSkatePoseShoot");
                state = HPState.Idle;
                // Set the state to Idle to
                // allow for transition
                break;

            case HPState.Celebrating:
                hpAnim.Play("Score");
                state = HPState.Idle;
                // Set the state to Idle to
                // allow for transition
                break;
        }
    }

    public void hpShoot()
    {
        state = HPState.Shooting;
    }

    public void hpCelebrate()
    {
        state = HPState.Celebrating;
    }
}

Here is the GameController script that calls the hpShoot and hpCelebrate functions to work with the animations:

public class GameController : MonoBehaviour {

    GameObject hp1, hp2, hp3;
    HockeyPlayerController hp1Control, hp2Control, hp3Control;
    public bool goalScored;
    bool shooterReady;
    float timer;
    int goalsAllowed, saves;

    void Start ()
    {
        // Find the hockey players
        hp1 = GameObject.FindGameObjectWithTag("HockeyPlayer1");
        hp2 = GameObject.FindGameObjectWithTag("HockeyPlayer2");
        hp3 = GameObject.FindGameObjectWithTag("HockeyPlayer3");

        // Get hockey players' controller scripts
        hp1Control = hp1.GetComponent("HockeyPlayerController") as HockeyPlayerController;
        hp2Control = hp2.GetComponent("HockeyPlayerController") as HockeyPlayerController;
        hp3Control = hp3.GetComponent("HockeyPlayerController") as HockeyPlayerController;

        shooterReady = true;
    }

    void Update()
    {
        if (shooterReady)
        {
            StartCoroutine(HockeyPlayerTurn());
        }
    }

    // Select a hockey player to shoot
    void HockeyPlayerSelection()
    {
        shooterReady = false;

        System.Random rand = new System.Random();
        int num = rand.Next(2,3);

        if (num == 1)
        {
            hp1Control.hpShoot();
            if (goalScored)
            {
                hp1Control.hpCelebrate();
                goalScored = false;
            }
        }
        else if (num == 2)
        {
            hp2Control.hpShoot();
            Debug.Log("Check If Goal Scored");
            if (goalScored)
            {
                hp2Control.hpCelebrate();
                goalScored = false;
            }
        }
        else if (num == 3)
        {
            hp3Control.hpShoot();
            if (goalScored)
            {
                hp3Control.hpCelebrate();
                goalScored = false;
            }
        }
    }

    IEnumerator HockeyPlayerTurn()
    {
        HockeyPlayerSelection();
        yield return new WaitForSeconds(8);
        shooterReady = true;
    }

Right now I am testing with Hockey Player 2. Calling the hpShoot function works as I want it to, however, when calling the hpCelebrate function, it waits until after the goalScored = false to play, yet I tested the bool goalScored and it turns to true right away so I know there is no issue with that. Do you see anything wrong that could be better?

bump

Hi there!! I was having a similar issue, where I had one animation that was playing late. Turns out it was actually playing slower than the other animations, but not because I had set it to play slower in the inspector. As it turns out, it was actually exported that way from Blender. The frame rate for that animation was 24 FPS whereas the animation that I needed it to match up with was playing at 30 FPS. I don’t know if this helps or not.