looping everything in the Update()

How can I loop these countdowns and set them to increase in speed over time?

    void Update ()
    {

        countdown -= Time.deltaTime;
        if(countdown <= 0.0f)
        {

       
            anim.SetBool("Shooting" , true);
            Shooting = true;


           
        }

        countdown2 -= Time.deltaTime;
        if (countdown2 <= 0.0f)
        {


            anim.SetBool("Shooting" , false);
            Shooting = false;


}
    void Update ()
    {

        countdown -= Time.deltaTime * countdownSpeed; //<-- multiply by a speed variable
        if(countdown <= 0.0f)
        {

     
            anim.SetBool("Shooting" , true);
            Shooting = true;
countdown = 10; //<-- reset counter to some value
countdownSpeed = countdownSpeed + 1; //<-- increase speed variable


         
        }
1 Like

Awesome hpjohn!! Thanks for the help.

Hi again, I’ve tried playing around with it a little and for some reason when the timer is reset to 3 seconds and gets back down to 0 the second time nothing happens in terms of the shooting animation. I have added a few coroutines to be fair so maybe thats why it doesn’t work. Well the timer sort of works but not the loop of the animation and coroutines. I say sort of works because it only loops once and then goes in to negative numbers. This code is only with the countdown reset to test.

    countdown -= Time.deltaTime;
        if (countdown <= 0.0f && cooldownTimer <= 0)
        {



       
                        anim.SetBool ("Shooting", true);
                        Shooting = true;

                        cooldownTimer = fireDelay;


                        Vector3 offset = transform.rotation * bulletOffset;

                        Instantiate (bulletPref, transform.position + offset, transform.rotation);
   
                        StartCoroutine ("pause");
                        StartCoroutine ("pause2");

                        countdown = 3;

           
        }

In the second countdown the layout is simpler and the countdown resets in a continuous loop as expected:

        countdown2 -= Time.deltaTime;
        if (countdown2 <= 0.0f)
        {


            anim.SetBool("Shooting" , false);
            Shooting = false;

            countdown2 = 6; //<-- reset counter to some value

}

Can you maybe tell me why the animations don’t replay when the timer has reset and why the timer in “countdown” only resets once? I’d really appreciate your help

In the first example, Does the animator bool reset to false at any point? it if remains true and you are just trying to set to each loop, nothing will change
Does it work without the coroutines, something in them affecting the values of cooldown or countdown?

Ah yes of course. That fixed the first issue. I put the countdown reset after the bool is deactivated and now both countdown timers reset regardless of the coroutines. But the animations and instantiations still don’t repeat/loop even though the timer is reset. Do you know why? here’s the whole update function i want looped :

    void Update ()
    {

        countdown -= Time.deltaTime;
        if (countdown <= 0.0f && cooldownTimer <= 0)
        {



       
                        anim.SetBool ("Shooting", true);
                        Shooting = true;

                        cooldownTimer = fireDelay;


                        Vector3 offset = transform.rotation * bulletOffset;

                        Instantiate (bulletPref, transform.position + offset, transform.rotation);
   
                        StartCoroutine ("pause");
                        StartCoroutine ("pause2");

                       

           
        }



        countdown2 -= Time.deltaTime;
        if (countdown2 <= 0.0f)
        {


            anim.SetBool("Shooting" , false);
            Shooting = false;


            countdown = 3;
            countdown2 = 5;

}
   

}

No.

There are some things you can do to discover the issue for yourself
start with

void Update ()
    {
        countdown -= Time.deltaTime;
        if (countdown <= 0.0f && cooldownTimer <= 0)
        {
              Debug.Log("Condition 1 triggered at " + Time.time);
///..........
        }
 
        countdown2 -= Time.deltaTime;
        if (countdown2 <= 0.0f)
        {
              Debug.Log("Condition 2 triggered at " + Time.time);
///...........
        }
}

Now you can see if they really are triggering when you think they are
then do

///..........
    Debug.Log("About to instantiate prefab " + bulletPref + " at position " + transform.position + offset);
                        Instantiate (bulletPref, transform.position + offset, transform.rotation);
///..........

because maybe its spawning in a place you aren’t expecting, or maybe the prefab has gone null for unknown reasons

The debug log said the countdown only triggered once while the countdown2 was looping. I then looked at the cooldownTimer = fireDelay; and realised it was supposed to be countdown = fireDelay; now it repeats :smile:DD Thank you for the advice and help hpjohn, you’ve been a great help! the debugging was key