Shooting in interval and waiting

Hello Guys

Please, I need assistance to resolve this issue with Coroutine since I’m not yet good at it. I’ve been on it since 4: 45 AM till now.

My goal is the shoot at an interval, say 10 secs and afterwards, have a cooldown for maybe 5 secs prior to restarting the function again. One of the reasons why I’ve got the variables set that way is that, at a later time in the game, there’ll be power-up to lessen the time the player has to wait before they can shoot again.

Here’s the script:

{

public bool canShoot;
public bool isShooting;

public float shoot_duration;            //say 10.0f
public float shootDelay;                   //say 5.0f

Bullet bullet;
PlayerInput input;
PlayerCharacter character;

Animator animator;
int ShootingParamID;

private void Awake()
{
    bullet          = GetComponent<Bullet>();
    animator        = GetComponent<Animator>();
    input           = GetComponent<PlayerInput>();
    character       = GetComponent<PlayerCharacter>();

    canShoot        = true;
    ShootingParamID = Animator.StringToHash("isShooting");
}

// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    animator.SetBool(ShootingParamID, isShooting);
}

private void FixedUpdate()
{
    GunsCheck();
}

void GunsCheck()
{
    if (character.isHanging || (!input.shootingHeld && isShooting))
        isShooting = false;

    if (shoot_duration <= 10 && canShoot)
    {
        if (input.shootingHeld && !isShooting)
        {
            StartCoroutine(Shoot());
            canShoot = false;
        }
    }
}

IEnumerator Shoot()
{
    isShooting = true;
    yield return new WaitForSeconds(shoot_duration); //Here I think, is where I'm having the problem. It's understandably adding the shoot_duration to the shoot_delay

    StartCoroutine(ShootCooldown());
}

IEnumerator ShootCooldown()
{
    yield return new WaitForSeconds(shootDelay);
    canShoot = true;
}

}

So far, the shoot works, the cooldown as well but there’s no control over the shoot duration

Hmm, unfortunately I’m not gonna be at a computer with Visual Studio for a little while, which makes my life difficult, but it looks to me like shoot_duration is never going to be less than 10, which might be a problem given the line if (shoot_duration <= 10 && canShoot). You might be able to get away with doing a while() loop during your shoot enum. Something along the lines of:

if(isShooting && shotTime <= shootDuration)
{
  while(isShooting)
  {
  shotTime++;
  yield return new WaitForSeconds(1);
  }
}
if(shotTime > shootDuration)
{
canShoot = false;
ShootCooldown();
}