Unity – Shooting with series

I thought it might be handy to make my enemy shoots few bullets at the same time. It’s a basic script without fancy conditions so I hope you will have no problems to understand it. I’m using InvokeRepeating for looping the shoot and then I use coroutines to make every shoot having a couple of bullets shooted one after another.

    [SerializeField] GameObject bulletPrefab;
    void Start()
    {
        InvokeRepeating("Shooter", 1f, 3f);
    }

    void Shooter(){
        StartCoroutine(Shoot());
    }

    IEnumerator Shoot()
    {
        int i = 0;
        while(i < 4){
            GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity);
            Destroy(bullet, 3f);
            yield return new WaitForSeconds(0.1f);
            i++;
        }
    }

Obviously, you can use for loop if you wish. Below is attached the same script as below but with added variables to define how many bullets should be shoot in the series and at what rate.

public class EnemyController : MonoBehaviour
{
    [SerializeField] GameObject bulletPrefab;
    float enemyShootingFrequency = 3f;
    int howManyBulletsInTheSeries = 4;
    float bulletsInSeriesDelay = 0.1f;
    void Start()
    {
        InvokeRepeating("Shooter", 1f, enemyShootingFrequency);
    }

    void Shooter(){
        StartCoroutine(Shoot());
    }

    IEnumerator Shoot()
    {
        int i = 0;
        while(i < howManyBulletsInTheSeries){
            GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity);
            Destroy(bullet, 3f);
            yield return new WaitForSeconds(bulletsInSeriesDelay);
            i++;
        }
    }

}