Search Unity

Question Animator.setInteger doesn't update animation when called inside coroutine

Discussion in 'Animation' started by ProjectEntropy, Feb 22, 2023.

  1. ProjectEntropy

    ProjectEntropy

    Joined:
    Nov 11, 2022
    Posts:
    9
    I am trying to create 2 combo attack. My animator is set up pretty simple like below:




    Basically, there is a parameter "atkComboStep" which determines the transition from the idle state to Attack1, to Attack2. There is no exit time when transitioning between them

    In my code, I have the following:

    Basically, I run a coroutine which lasts for 0.65 seconds, the same length as Attack1. If the user presses the attack again during that time, I toggle a variable _clickedWhileAttack to true. And when the coroutine finishes, it determines whether to run the second attack animation, or reset everything back to idle.

    Code (CSharp):
    1.  
    2. private void attack(InputAction.CallbackContext obj)
    3.     {
    4.         if (_comboAttackCoroutine == null) {
    5.             _comboHitStep++;
    6.             _animator.SetInteger("atkComboStep", _comboHitStep);
    7.             _comboAttackCoroutine = StartCoroutine(comboAttack());
    8.         } else {
    9.             _clickedWhileAttacking = true;
    10.         }
    11.         _animator.SetBool("isRunning", false);
    12.     }
    13.  
    14.     private IEnumerator comboAttack() {
    15.         yield return new WaitForSeconds(0.65f);
    16.  
    17.         if (_clickedWhileAttacking == true && _comboHitStep < 2) {
    18.             _comboHitStep++;
    19.             _animator.SetInteger("atkComboStep", _comboHitStep);
    20.             Debug.Log($"Executing inside if statement, current comboHitStep is {_comboHitStep}");
    21.  
    22.             yield return new WaitForSeconds(0.5f);
    23.              _comboHitStep = 0;
    24.             _animator.SetInteger("atkComboStep", _comboHitStep);
    25.         } else {
    26.             _comboHitStep = 0;
    27.             _animator.SetInteger("atkComboStep", _comboHitStep);
    28.             _clickedWhileAttacking = false;
    29.  
    30.         }
    31.     }
    The problem is: my second attack never happens! I used a Debug.Log above and it fires correctly, and logs the correct comboHitStep as 2. Yet, despite this on screen my character just stands there after finishing the first attack. (In fact, if I press twice he also never transitions back to idle, and now just glides around the floor as well).
    It seems updating _animator.SetInteger doesn't work inside Coroutines? Even though the variable "atkComboStep" is updated, somehow the animation based on that parameter is never triggered?
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    I don't think we're getting the full picture from what you've posted, so there may be other issues, but I got it working in my re-creation.

    The first issue I saw was that "_comboAttackCoroutine" was never set back to null, so line 4 is always false after the first attack. This makes it so that you can't attack anymore after attacking once.

    The second issue was that "_clickedWhileAttacking" isn't set back to false if line 17 is true. This causes the full combo to always play out if it's been played before.

    The way I solved these issues was to insert this code at line 29:
    Code (CSharp):
    1. _comboAttackCoroutine = null;
    2. _clickedWhileAttacking = false;
    There may be other issues, and I may have missed something in my re-creation of your code, but the above changes got it working for me.