Zoubaier Aouadi
Zoubaier Aouadi

Reputation: 23

In unity calling a coroutine that include playing animation inside another coroutine make the animation play only 0.1 sec

I have a project that read inputs from Arduino controllers via bluetooth and play a specific animation based on what inputs it read, the inputs can be 1 or 2 or 3 or a mix between 2 or 3 of them, a total of 7 inputs.

each reading is inside an "if" statement then "else if" one for checking next combination, im reading a coroutine that play an animation and the problem here is that it plays only 0.1 second then cut.

I tried to add a condition to wait until animation end then read again but that didint work eithe, i will add a small exmaple

IEnumerator  ManageConnection (BluetoothDevice device)
{   
    while (device.IsReading) 
    {
        byte [] msg = device.read ();

        if (msg != null) 
        {
            string content = System.Text.ASCIIEncoding.ASCII.GetString (msg);

            string [] lines = content.Split(new char[]{'\n','\r'});

            //Add those lines to the scrollText
            if (!animationPlaying)
            {
                if (lines.Contains("123"))
                {
                    StartCoroutine(Animating(0));
                    audsrc.PlayOneShot(audioList[5]);
                    StartCoroutine(SlowDown(0.1f));
                    //StartCoroutine(ActiveEffect(5));

                    coinsCount += 1;
                }
                else if (lines.Contains("12"))
                {
                    yeahCount += 1;
                    StartCoroutine(Animating(5));
                    audsrc.PlayOneShot(audioList[4]);
                    StartCoroutine(SlowDown(0.1f)); 
                }   
            }
        }

And this one is for the Animating Coroutine :

public IEnumerator Animating(int i)
{
    animationPlaying = true;
    animator.Play(animationNames[i]);

    yield return new WaitForSeconds(1);

    animationPlaying = false;
}           

I just want the animation to play until the end ot just 0.1 second.

Thank you.

Upvotes: 1

Views: 602

Answers (1)

Nícolas
Nícolas

Reputation: 416

you could make the animation play until the end on the Animator Component instead of hard coding with coroutines.

here is a link to the unity docs

Upvotes: 0

Related Questions