2D Sprite flickers everytime my animation resets

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.

It’s probably because you have your idle state or AnyState transitioning to shoot, and the shoot boolean is still true so the animator is continuously transitioning to the shoot state.

Try using trigger parameters instead, which are essentially booleans which will be true until a transition uses it, then automatically turns false and won’t transition again until set true again.

1 Like

Thanks for the response. I manage to fix the issue by trial and error. I still stick with booleans though, it seems like there is something wrong with the conditions and transitions that I used in my mecanim.

1 Like