Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animation Playing Late

Discussion in 'Scripting' started by tburns517, Jun 9, 2016.

  1. tburns517

    tburns517

    Joined:
    Jan 12, 2016
    Posts:
    48
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HockeyPlayerController: MonoBehaviour
    5. {
    6.     private enum HPState
    7.     {
    8.         Idle,
    9.         Shooting,
    10.         Celebrating,
    11.     }
    12.  
    13.     private HPState state = HPState.Idle;
    14.     private Animation hpAnim;
    15.  
    16.     void Start()
    17.     {
    18.         hpAnim = GetComponent<Animation>();
    19.         state = HPState.Idle;
    20.     }
    21.  
    22.  
    23.     void Update()
    24.     {
    25.         switch (state)
    26.         {
    27.             case HPState.Idle:
    28.                 // Check to see that other animations
    29.                 // are not playing before going to Idle
    30.                 if (!hpAnim.IsPlaying("SlowSkatePoseShoot") && !hpAnim.IsPlaying("Score"))
    31.                 {
    32.                     hpAnim.Play("Idle");
    33.                 }
    34.                 break;
    35.  
    36.             case HPState.Shooting:
    37.                 hpAnim.Play("SlowSkatePoseShoot");
    38.                 state = HPState.Idle;
    39.                 // Set the state to Idle to
    40.                 // allow for transition
    41.                 break;
    42.  
    43.             case HPState.Celebrating:
    44.                 hpAnim.Play("Score");
    45.                 state = HPState.Idle;
    46.                 // Set the state to Idle to
    47.                 // allow for transition
    48.                 break;
    49.         }
    50.     }
    51.  
    52.     public void hpShoot()
    53.     {
    54.         state = HPState.Shooting;
    55.     }
    56.  
    57.     public void hpCelebrate()
    58.     {
    59.         state = HPState.Celebrating;
    60.     }
    61. }  


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

    Code (CSharp):
    1. public class GameController : MonoBehaviour {
    2.  
    3.     GameObject hp1, hp2, hp3;
    4.     HockeyPlayerController hp1Control, hp2Control, hp3Control;
    5.     public bool goalScored;
    6.     bool shooterReady;
    7.     float timer;
    8.     int goalsAllowed, saves;
    9.  
    10.     void Start ()
    11.     {
    12.         // Find the hockey players
    13.         hp1 = GameObject.FindGameObjectWithTag("HockeyPlayer1");
    14.         hp2 = GameObject.FindGameObjectWithTag("HockeyPlayer2");
    15.         hp3 = GameObject.FindGameObjectWithTag("HockeyPlayer3");
    16.  
    17.         // Get hockey players' controller scripts
    18.         hp1Control = hp1.GetComponent("HockeyPlayerController") as HockeyPlayerController;
    19.         hp2Control = hp2.GetComponent("HockeyPlayerController") as HockeyPlayerController;
    20.         hp3Control = hp3.GetComponent("HockeyPlayerController") as HockeyPlayerController;
    21.  
    22.         shooterReady = true;
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         if (shooterReady)
    28.         {
    29.             StartCoroutine(HockeyPlayerTurn());
    30.         }
    31.     }
    32.  
    33.     // Select a hockey player to shoot
    34.     void HockeyPlayerSelection()
    35.     {
    36.         shooterReady = false;
    37.  
    38.         System.Random rand = new System.Random();
    39.         int num = rand.Next(2,3);
    40.  
    41.         if (num == 1)
    42.         {
    43.             hp1Control.hpShoot();
    44.             if (goalScored)
    45.             {
    46.                 hp1Control.hpCelebrate();
    47.                 goalScored = false;
    48.             }
    49.         }
    50.         else if (num == 2)
    51.         {
    52.             hp2Control.hpShoot();
    53.             Debug.Log("Check If Goal Scored");
    54.             if (goalScored)
    55.             {
    56.                 hp2Control.hpCelebrate();
    57.                 goalScored = false;
    58.             }
    59.         }
    60.         else if (num == 3)
    61.         {
    62.             hp3Control.hpShoot();
    63.             if (goalScored)
    64.             {
    65.                 hp3Control.hpCelebrate();
    66.                 goalScored = false;
    67.             }
    68.         }
    69.     }
    70.  
    71.     IEnumerator HockeyPlayerTurn()
    72.     {
    73.         HockeyPlayerSelection();
    74.         yield return new WaitForSeconds(8);
    75.         shooterReady = true;
    76.     }

    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?
     
  2. tburns517

    tburns517

    Joined:
    Jan 12, 2016
    Posts:
    48
  3. aialexander3d

    aialexander3d

    Joined:
    Jul 28, 2017
    Posts:
    3
    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.