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

Question Solved. Wait until animator state finishes in monobehaviours coroutine.

Discussion in 'Animation' started by SamTyurenkov, Sep 25, 2023.

  1. SamTyurenkov

    SamTyurenkov

    Joined:
    May 12, 2018
    Posts:
    83
    Hi, im trying to implement:

    Monobehaviour attack coroutine, which sets animator bool parameter "Attack" to true. After setting it to true, state machine transitions to "Attack In" state. In couroutine I want to wait for the Attack In state to finish before dealing damage. And then wait for Attack out state to finish before starting a new attack loop.

    Tried various approaches through accessing animator directly with GetCurrentStateInfo, but it seems they are out of current frame or something.

    Should I use statebehaviour and somehow connect it with attack coroutine in monobehaviour?

    What I tried so far:


    Code (CSharp):
    1.     private IEnumerator AttackCoroutine(DamagableUnit target)
    2.     {
    3.  
    4.  
    5.         while (target.IsAlive() && damagableUnit.IsAlive())
    6.         {      
    7.  
    8.    //OVERRIDE ANIMATION WITH WEAPON SPECIFIC ANIMATION
    9.             if (availableWeaponsPool.activeWeapon.animatorOverrideController)
    10.             {
    11.                 anim.runtimeAnimatorController = availableWeaponsPool.activeWeapon.animatorOverrideController;
    12.             }
    13.             else
    14.             {
    15.                 anim.runtimeAnimatorController = originalController;
    16.             }
    17.             anim.SetFloat("AttackInMultiplier", attackInMultiplier);
    18.             anim.SetFloat("AttackOutMultiplier", attackOutMultiplier);
    19.             anim.SetBool("Attack", true);
    20. ;        
    21.  
    22.  
    23.             //DONT DEAL DAMAGE UNTIL ATTACK IN ANIMATION IS PLAYED
    24.             yield return new WaitForEndOfFrame();
    25.             while (HasStateFinished("Attack In")) {
    26.                 Debug.Log("state was attack in");
    27.                 yield return null;
    28.             }
    29.             //SEND DAMAGE TO DAMAGABLE
    30.             target.TakeDamage(dmg, damagableUnit);
    31.  
    32.             //DONT START A NEW ATTACK LOOP WHILE ANIMATION OUT IS PLAYING
    33.             yield return new WaitForEndOfFrame();
    34.             while (HasStateFinished("Attack Out")) {
    35.                 Debug.Log("state was attack out");
    36.                 yield return null;
    37.             }
    38.  
    39.             anim.runtimeAnimatorController = originalController;
    40.             anim.SetBool("Attack", false);
    41.             attacking = false;
    42.             movableUnit.agent.canMove = true;
    43.         }
    44.  
    45.         StopCoroutine(attackCoroutine);
    46.     }
    47.  
    48.     private bool HasStateFinished(string targetState)
    49.     {
    50.         // var hash = Animator.StringToHash("Base."+targetState);
    51.          var state = anim.GetCurrentAnimatorStateInfo(0);
    52.         // return state.fullPathHash == hash;
    53.         return state.IsName(targetState);
    54.     }
     
    Last edited: Sep 25, 2023
  2. SamTyurenkov

    SamTyurenkov

    Joined:
    May 12, 2018
    Posts:
    83