So I made a simple shooting script with a cooldown timer. For the animation, I am using mecanim.
Everything works fine except for a slight issue. Whenever the cooldown timer reaches 0, the shooting animation automatically resets, that causes my sprite to slightly flicker. The shooting states keeps on resetting even if the player is in a different animation state.
Here’s my script:
private float attackTimer;
private float attackCoolDown = 1f;
private Animator p_Anim;
private bool isAttacking;
void Update () {
Shoot ();
}
//Shooting Function
void Shoot () {
if (Input.GetMouseButton (0) && !isAttacking) {
isAttacking = true;
attackTimer = attackCoolDown;
p_Anim.SetBool ("Shoot", true);
}
if (isAttacking) {
if (attackTimer > 0) {
// start countdown until it reaches zero...
attackTimer -= Time.deltaTime;
}
else if (attackTimer < 0) {
isAttacking = false;
p_Anim.SetBool ("Shoot", false);
p_Anim.SetBool ("RunNShoot", false);
p_Anim.SetBool ("RunBackWard", false);
}
}
So how can I get rid of that flickering stuff? Been looking for the solution for days now but still no luck.