Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Animation play not doing anything the second time?

Discussion in 'Scripting' started by Aseliot, May 30, 2021.

  1. Aseliot

    Aseliot

    Joined:
    May 7, 2019
    Posts:
    35
    Yeah I know i've been reading a lot of stuff about this animator already and I've already tried setTrigger, it does nothing, and setBool just keeps repeating the animation over and over.

    What I was trying to do was just have one animation that I trigger via script but it doesn't do anything the second time and the animation window shows no feedback about what is going on...

    I am sure the mainCooldownRoutine works and state is set to ready.

    Code (CSharp):
    1.   public override void main(bool toggle)
    2.     {
    3.         base.main(toggle);
    4.         if (state == State.Ready)
    5.         {
    6.             damageCollider.enabled = true;
    7.             animator.speed = animationSpeed;
    8.  
    9.             string animationName = "MeleeSwingRightToLeft";
    10.             animator.Play(animationName);
    11.  
    12.             if (mainAudioClip != null) audioSource.PlayOneShot(mainAudioClip);
    13.             StartCoroutine(mainCooldownRoutine(animationName));
    14.         }
    15.     }
    16.  
    17.     public IEnumerator mainCooldownRoutine(string animationName)
    18.     {
    19.  
    20.         state = State.Cooldown;
    21.         yield return new WaitForSeconds(mainCooldown / 2);
    22.         if (cooldownAudioClip != null) audioSource.PlayOneShot(cooldownAudioClip);
    23.         yield return new WaitForSeconds(mainCooldown / 2);
    24.    
    25.         while (true)
    26.         {
    27.             AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(0);
    28.             if (info.IsName(animationName) && info.normalizedTime >= 1.0f) break;
    29.             else yield return new WaitForSecondsRealtime(0.05f);
    30.         }
    31.  
    32.         Debug.Log("DONE");
    33.  
    34.         state = State.Ready;
    35.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Generally you use setTrigger to start the animation, and configure a transition out of that animation and mark it as "Has Exit Time."

    See here my attack -> idle transition. No condition, just "Has Exit Time"

    Screen Shot 2021-05-30 at 7.01.01 AM.png
     
  3. Aseliot

    Aseliot

    Joined:
    May 7, 2019
    Posts:
    35
    Ah alright got it working now with setTrigger/resetTrigger not sure what was going on before, either way when using it like this the info.IsName(animationName) condition will always return false so I just removed it for now.