Plane drops altitude, canon fires, but no shell

I have my flak canon firing and then a second later the flak explosion hits the air
sure i think i probably did it very clumsily but it works.

i have the canon shotting at the player above a certain altitude. And it works great but the only problem is:

when the player is diving, the canon will shott, but then no explosion because the player has obviously dived below the firing threshold.

Any ideas welcomed on how to fix this problem.

heres da code:

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

public class FlakCanonController: MonoBehaviour
{
    public Transform target;
    private Rigidbody2D rb;
    public float rotateSpeed = 200f;
    public GameObject canonFires;
    public GameObject flakPrefab;
    public GameObject bulletSpawnObject;
    private bool isAlive = true;
    public float FlakSpeed = 0;

    private bool shotWasFired = false;
    private bool canShoot = true;
    private bool flakExplodes = false;
   

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }
    void FixedUpdate()
    {
        Vector2 direction = (Vector2)target.position - rb.position;

        direction.Normalize();

        float rotateAmount = Vector3.Cross(direction, transform.up).z;

        rb.angularVelocity = -rotateAmount * rotateSpeed;

        if (target.position.y > 1 && isAlive)
        {
            if (canShoot)
            {
                FindObjectOfType<AudioManager>().Play("FlakShooting");
                StartCoroutine(TimeDelay());
                canShoot = false;
                StartCoroutine(TimeDelay2());
            }      

            if (flakExplodes)
            {
                GameObject newFlakExplosion = Instantiate(flakPrefab, bulletSpawnObject.transform.position, transform.rotation);
                newFlakExplosion.GetComponent<Rigidbody2D>().AddRelativeForce(Vector2.up * FlakSpeed);
                Destroy(newFlakExplosion, 1.0F);
                FindObjectOfType<AudioManager>().Play("FlakExplode");
                flakExplodes = false;
            }

           
        }
    }


    IEnumerator TimeDelay()
    {
        yield return new WaitForSeconds(3);
        canShoot = true;
       

    }

    IEnumerator TimeDelay2()
    {
        yield return new WaitForSeconds(1);
      
        flakExplodes = true;
    }

}





   
}*/

It kinda comes down to what behavior you want.

Do you want a flak shot fired when the player is high to simply miss when the player gets lower mid-flight?

Or do you just want the flak shot to always hit, and only fire when the player is above the requisite altitude?

Or something else?

at the moment the flak is just hit and miss and it works well, but what happens is if a shot is fired…then the player goes down below the line, you hear the canon fire, but no flak shot appears till you fly up again. I think i have this poorly designed, its like a follow on. not really two separate events so to speak.

I would just make the flak explode harmlessly directly above where the player is (at the minimum altitude for it)

ok will try