- Home /
 
 
               Question by 
               Shinindennnnn · May 13 at 10:12 PM · 
                coroutineenemyshooting  
              
 
              Please, I need help, I'm trying to create a Coroutine where my enemy shoots 1 pack of 4 bullets, every 2 seconds, but it just ended up in the enemy shooting some bullets and then never again
 IEnumerator Shoot()   
 {       
     while(quantBullet <= 4)        
     {
         GameObject bullet = Instantiate(bulletPrefab, shotSpawner.position, shotSpawner.rotation);         
         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();           
         rb.AddForce(shotSpawner.up * bulletForce, ForceMode2D.Impulse);          
         quantBullet++;           
         yield return new WaitForSeconds(2f);
       
         if (quantBullet == 4)
         {
             pack = true;
         }
         if(pack == true)
         {
             yield break;
         }
         yield return null;
     }
 }
 
              
               Comment
              
 
               
              Answer by breban1 · May 14 at 02:18 PM
 IEnumerator Shoot()   
 {       
     while(true)
     {
         quantBullet = 0;
         while(quantBullet < 4)        
         {
             GameObject bullet = Instantiate(bulletPrefab, shotSpawner.position, shotSpawner.rotation);         
             Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();           
             rb.AddForce(shotSpawner.up * bulletForce, ForceMode2D.Impulse);          
             quantBullet++;           
             yield return new WaitForSeconds(.2f);
         }
         
         yield return new WaitForSeconds(2.0f);
     }
 }
 
               Only call StartCoroutine(Shoot()) one time per enemy.
Your answer