every time I call the IEnumerator function to wait then play an animation it play it two times ,Why?
void Update()
{
StartCoroutine(FightingAnim());
}
IEnumerator FightingAnim ()
{
bool keyPressed = Input.GetKey(KeyCode.J);
if (keyPressed)
{
timer += Time.deltaTime;
attacking = true;
if (timer >= 1.0f)
{
m_Anim.SetBool("Holding", true);
attacking = true;
}
if (timer >= 4.0f)
{
Debug.Log("Ready!");
}
}
else if (timer >= 0.1f && timer < 4.0f)
{
m_Anim.SetTrigger("Attacking");
m_Anim.SetBool("Holding", false);
yield return new WaitForSeconds(1);
// so the player can't move while attacking
attacking = false;
timer = 0;
}
else if (timer >= 4.0f)
{
m_Anim.SetTrigger("Punch!");
m_Anim.SetBool("Holding", false);
attacking = false;
timer = 0;
Rigidbody2D instantiatedFist = Instantiate(m_Punch, transform.position, transform.rotation) as Rigidbody2D;
instantiatedFist.velocity = transform.TransformDirection(new Vector3(5, 0, 0));
Physics2D.IgnoreCollision(instantiatedFist.GetComponent<Collider2D>(), GetComponent<Collider2D>());
Destroy(GameObject.Find("PunchRocket(Clone)"), 5f);
}
}