- Home /
Coroutine / Switch case
Animator anim; int inputNum;
public IEnumerator PlayAnims()
{
switch (inputNum)
{
case 1:
anim.Play("1");
Debug.Log("1");
yield return new WaitForSeconds(1);
break;
case 2:
anim.Play("2");
Debug.Log("2");
yield return new WaitForSeconds(1);
break;
case 3:
anim.Play("3");
Debug.Log("3");
yield return new WaitForSeconds(1);
break;
}
}
So the idea is that it should only play one animation at a time for 1 second then play the next one for 1 second etc. If it gets multiple inputs at once it picks the one which was first, like a queue, so if the input is 2,1,2,3 then they play those animations in that order 1 second at a time before the next animation starts.
Not easy to answer when we don't know how this Coroutine is being called or how inputNum is being set.
Yes I understand that. I'm just not sure how to explain it. You would type in the input so if you type in 2223 then the second animation would play three times after another and then the third animation last. So the StartCoroutine would contain the inputNum.
Answer by HellsHand · May 01, 2021 at 04:30 PM
Ok lemme put it this way. Nothing in your Coroutine allows for anything more than one animation to play. If you try to stick it in a loop and call it for each value, all animations would technically be called at the same time not 1 each second since a loop completes in it's entirety in 1 single frame and there can be multiple Coroutines running at the same time. That said if the loop were inside the Coroutine that would be a different story. Such as:
public IEnumerator PlayAnims(string inputString)
{
char[] inputs = inputString.ToCharArray();
foreach (char inputNum in inputs)
{
switch (int.Parse(inputNum.ToString()))
{
case 1:
anim.Play("1");
Debug.Log("1");
yield return new WaitForSeconds(1);
break;
case 2:
anim.Play("2");
Debug.Log("2");
yield return new WaitForSeconds(1);
break;
case 3:
anim.Play("3");
Debug.Log("3");
yield return new WaitForSeconds(1);
break;
}
}
}
I want only one animation to play. But I want to input my numbers, the animation correspondent to the first number play, waitForSeconds, then play the next one. I.e if I type in "2, 2, 3, 1" I want animation 2 to play, wait, then play 2 again, wait, then 3, wait, then 1.
That is what the code provided does, it takes a string of digits(you would have to ensure they are digits though) and calls the case corresponding to the first digit, waits for 1 second, calls the next case that corresponds with the next digit, waits for a second and so on. If this isn't what your asking for you may need to provide further information. First question that comes to $$anonymous$$d is are all these animations a second long? If not does it need to wait for the current animation to finish before calling on the next(though I guess it should)?
What happens is that it plays the animations but not in the right order. So if I press 2,2,3 it plays it back as 2, 3, 2. If I press 2,1,1,1 it plays it back as 2,1,2,1. Do you know why?