How do I add 2 delays in the same script?

Hello all!

I’m still working on a Unity project, and I’ve ran into another issue!

I’m currently making an enemy that fires a “burst” of two shots in rapid succession. There’s a 3 second delay between each burst, and then the first shot is meant to fire 0.5 seconds before the second one.

The only problem is I can’t seem to add two delays in one script!

This is the script that controls the firing of my “Burst Enemy”:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BurstEnemyGun : MonoBehaviour
{
    //define variables
    public Transform enemybulletspawnpoint;
    public GameObject enemybulletprefab;
    public float enemybulletspeed = 10;
    public float enemyshootTimer = 0;
    public float enemyshootdelay = 3;
    public float timebetweenshots = 0;
    public float delaybetweenshots = 0.5f;
    void Update()
    {
        //time comparison
        enemyshootTimer += Time.deltaTime;
        if (enemyshootTimer > enemyshootdelay){
            enemyshootTimer = 0;
            //instantiate bullet
            var enemybullet = Instantiate(enemybulletprefab, enemybulletspawnpoint.position, enemybulletspawnpoint.rotation);
            //add velocity
            enemybullet.GetComponent<Rigidbody>().velocity = enemybulletspawnpoint.forward * enemybulletspeed;
            timebetweenshots += Time.deltaTime;
            if (timebetweenshots > delaybetweenshots){
                timebetweenshots = 0;
                var enemybullet2 = Instantiate(enemybulletprefab, enemybulletspawnpoint.position, enemybulletspawnpoint.rotation);
                //add velocity
                enemybullet2.GetComponent<Rigidbody>().velocity = enemybulletspawnpoint.forward * enemybulletspeed;
            }
        }
    }
}

Currently, using this code makes the enemy only shoot one bullet at a time, as opposed to two shots in rapid succession.

Any support is appreciated, thank you in advance!!

:slight_smile:

        timebetweenshots += Time.deltaTime;

Seems like this timer only goes when you shoot first bullet, which is, one frame.
One solution is to replace the timer in the main Update with an if (flag) statement, and make that flag toggling on when you shoot once, and ressetting it off when you shoot second time.