0
\$\begingroup\$

Ok to be specific, both of my enemies will continuously play their attack animation even when it is false. I don't understand what I am doing wrong here :(

void OnTriggerEnter2D(Collider2D other){
    //If attack collider is player and we're not attacking
    if (other.name == "Player" && !attacking) {
        attacking = true;
        attackTimer = attackCD;
    }
    if (attacking) 
    {
        if (attackTimer > 0)
        {
            Debug.Log ("Decreasing");
            attackTimer -= Time.deltaTime;
        }else if (attackTimer == 0) 
          {
            attacking = false;
            Debug.Log ("Done attacking!");
            //attackTimer = attackCD;
          } 
    }
}

anim.SetBool ("Attack", attacking);//This sets the animation but it is in my Update function not my OnTriggerEnter2D function
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

It could be that the attack timer never reaches 0 as the delta time will keep removing time which will eventually cause it to go negative. Instead check if the timer is less then or equal to 0 so if it goes negative the attack animation is stopped.

else if (attackTimer <= 0f)

Note: You should add an "f" behind numbers that you want to be floats because without the "f" the compiler thinks that you want a double which could waste memory if you have lots of them without needing the extra precision.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .