Using WaitForSeconds to wait for a animation to finish?

Hey all, heres my snippit of code. I am trying to play attack animation when you press P and set “attacking” to true. and leave it as true while the animation is playing. Also wanted to use the delay as a “stun” so the player doesnt move or do anything to cancel the animation while its still playing. but currently if I spam P I just keep interrupting the attack animation.

Am i using the delay wrong? I was under the impression that as soon as I set the delay with StartCoroutine nothing else in the code will run but my Update() seems to still be looping.

Thanks!

    Animator anim;

    public bool attacking = false;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator>();
    }
   
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown("p")) {
            attacking = true;
            anim.SetTrigger("Attack1Trigger");
            StartCoroutine (COStunPause(1.2f));
            attacking = false;
        }
    }

    public IEnumerator COStunPause(float pauseTime)
    {
        yield return new WaitForSeconds(pauseTime);
    }

Try to put “attack = false” inside “COStunPause”, after the line “yield return …”

hey there. thanks! that worked for me. except i still dont really understand why this wouldn’t work? if anyone can shed some light

Hi.

From the code, “attacking = false” will be executed immediately after “StartCoroutine(…)”
The coroutine means the function, “COStunPause” from the code. WaitForSeconds only works inside the coroutine.
You may find more example here:

OH that makes sense. thanks!