V
V
vovka_losira2021-07-21 23:09:15
Unity
vovka_losira, 2021-07-21 23:09:15

Why doesn't the coroutine work?

There is a function to switch the state of the character. I want the switching to happen only after the animation is completed. I made a coroutine, but it doesn't work (Please help.

public void Change_status(Status _status)
    {
        StartCoroutine(WaitForAnimation("SwordHit"));

        status = _status;
        if (_status == Status.Attack)
        {
            if (anim.GetBool(walkAnimName))
            {
                anim.SetBool(walkAnimName, false);
                anim.SetBool("isAttacking", true);
            }
        }
        else if (_status == Status.Walk)
        {
            if (!anim.GetBool(walkAnimName))
            {
                anim.SetBool(walkAnimName, true);
                anim.SetBool("isAttacking", false);
            }

        }
        else if (_status == Status.Idle)
        {
            if (!anim.GetBool(walkAnimName))
            {
                anim.SetBool(walkAnimName, false);
            }
        }
    }

    private IEnumerator WaitForAnimation(string _name)
    {
        while (anim.GetCurrentAnimatorStateInfo(0).IsName(_name))
        {
            yield return null;
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2021-07-21
@GavriKos

Well, the coroutine does nothing for you. She opened up and everything. And all the code after StartCoroutine will be executed immediately)

K
K1ald, 2021-07-22
@K1ald

You don't have a coroutine called "SwordHit" there...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question