- Home /
Countdown not working (instantiating on count<=0)
Hey. I made tower-defense automatic shooting system, the problem is that my bullets are non-stop created, and I saw that my fireCountdown float doesn't set back to the fireRate, and stays at 0 making my attack script non-stop-spawning bullets.
Do you have an idea of how to fix that ? The Update part code ;
 void Update () {
         if (target == null)
         {
             if (useLaser)
             {
                 if (lineRenderer.enabled)
                 {
                     lineRenderer.enabled = false;
                     Destroy(impactEffect);
                     impactLight.enabled = false;
                 }
             }
 
             return;
         }
 
         if (useLaser)
         {
             Laser();
         } 
         else // if using bullets
         {
             if (fireCountdown <= 0f)
             {
                 Shoot();
                 fireCountdown = fireRate;
             }
 
             fireCountdown -= Time.deltaTime;
         }
     }
Answer by Nakoru · Sep 16, 2021 at 07:43 PM
Fixed by moving the countdown line over the Shoot() call 0-0.
Answer by Kona · Sep 16, 2021 at 07:21 PM
Are you sure that your "fireRate" property is not set to 0? As far as I can see that code should work. Another way to do it is to cache the last time a bullet was fired, something like this:
 public float attackRate = 1f;  // Fire one projectile/sec
 private float _lastAttack;
 
 public void Awake() {
   _lastAttack = Time.time;
 }
 
 public void Update() {
   if( Time.time - _lastAttack >= attackRate ) {
     Shoot();   //Shoot a bullet when the target time has passed
     _lastAttack = Time.time;  //Cache current time again
   }
 }
Okay, I don't figure out why, but moving the line of the countdown = fireRate over the Shoot() call made it work. Thanks for your time tough, it's really nice of yours !
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                