I have a problem for a while now and I am running out of ideas: I have a made a coroutine used to implement animations in the attacks, something like this
private IEnumerator FirstAttack()
{
comboCounter = 1;
myAnimator.SetInteger("comboSequence", comboCounter);
currentState = PlayerState.attack;
yield return null;
yield return new WaitForSeconds(AttackTemplate.SetDuration(0.9f));
comboCounter = 0;
myAnimator.SetInteger("comboSequence", comboCounter);
currentState = PlayerState.walk;
}
This coroutine works perfectly fine. The problem occurs when I try to concatenate this with another coroutine to perform something like a combo sequence; to do so I basically add a while cycle that checks if I am pressing the attack button while there is a time opening for the second attack to connect. The result is this:
private IEnumerator FirstAttack() {
comboCounter = 1;
myAnimator.SetInteger("comboSequence", comboCounter);
currentState = PlayerState.attack;
yield return new WaitForSeconds(AttackTemplate.SetDuration(0.6f) - comboTimer);//Problem: if i reduce the time below the animation time of the second animation, the second animation won't go until the end
StartCoroutine(IntervalCounter());
comboCounter = 0;
myAnimator.SetInteger("comboSequence", comboCounter);
currentState = PlayerState.walk;
}
private IEnumerator SecondAttack()
{
comboCounter = 2;
myAnimator.SetInteger("comboSequence", comboCounter);
currentState = PlayerState.attack;
yield return null;
yield return new WaitForSeconds(AttackTemplate.SetDuration(0.9f));
comboCounter = 0;
myAnimator.SetInteger("comboSequence", comboCounter);
currentState = PlayerState.walk;
}
private IEnumerator IntervalCounter()
{
comboTimer = AttackTemplate.SetComboTimer(0.4f);
while (comboTimer >= 0)
{
Debug.Log(comboTimer);
comboTimer -= Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Debug.Log("Chained");
StartCoroutine(SecondAttack());
yield break;
}
yield return null;
}
}
My problem is that after adding the while loop the transition to the first animation keeps being updated, to be precise the one going from Any State to Basic Attack Tree in the following image.
If I press the attack button this transition will be repeated as long as the coroutine working. I tried to wrap around the first two lines in which i set the animator to do nothing if the integer managing the combo sequence, comboCounter
is already set to 1 (which is the value related to the first attack), like this
if (comboCounter != 1)
{
comboCounter = 1;
myAnimator.SetInteger("comboSequence", comboCounter);
}else if(comboCounter == 1) {
}
But it keeps repeating. I wanted to use something like WaitUntil, but I tried just to put it and having a bool always false, in order to suspend the coroutine indefinitely, but in that period it keeps updating the animator. Do you have any ideas why is that? How do I prevent a coroutine from running all the time?