[C#]Why Isn't This Working!

Basically I am creating an attack animation but I don’t want the player to be able to spam the attack button and attack very rapidly. So I “thought” the easiest way of fixing this issue is through an Coroutine … So I wrote this:

IEnumerator StartAttack()
	{
		yield return new WaitForSeconds(0.1666667f);
		playerSprite.DoAnim(1);
	}

very basic right? Looks like it should wait 0.16 seconds (time it takes for the attack animation to complete) but for some reason it doesn’t and I fail to realize why… I tried placing playerSprite.DoAnim(1); on the top and bottom of WaitForSeconds() but same results appear. Yes I did Initialize it in update()… Although probably could be that the wait time is to short? lol anyway any help will be very much appreciated. Thanks in advance :slight_smile:

EDIT: I tried increasing the wait time and it looks as though it works at first but if you spam attack really fast it look like it breaks the coroutine or something? Idk… maybe my attacking skills are just to boss :wink:

FIXED! For anyone wondering basically the coroutine was being called to quickly since I was starting it in Update(). So I had to creating a bool that did a check to see if I was currently attacking :slight_smile: Now I feel dumb…

Did you use StartCoroutine(StartAttack()); ?

Indeed I did kind sir :slight_smile:

You could use Debug like this, so you can see where and when the code is being executed.

IEnumerator StartAttack()
    {
        Debug.Log("Going to wait");
        yield return new WaitForSeconds(0.1666667f);
        Debug.Log("Done waiting, play anim");
        playerSprite.DoAnim(1);
    }

I’d suggest you use a bool for what you want. canAttack must be set to false when you fire then once the animation is done set can Attack to true again.

Thanks for suggestion bro! it would of been the correct fix but I must of figured it out as you were typing your response lololol