trying to make a weapon cooldown

this is my code

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

public class WeaponTest : MonoBehaviour
{
public float damage = 50f;
public float range = 100f;
public float impactForce = 50f;
public float weaponHeat;
public float weaponOverHeat = 100f;
public bool weaponIsOverHeat;

public Camera playerCam;
private PickUpWeapon pickUpScript;

// Start is called before the first frame update
void Start()
{
    //weaponHeat at the start was 0
    weaponHeat = 0;

    //getting pick up script from the player in the active scene
    pickUpScript = GameObject.Find("Player").GetComponent<PickUpWeapon>();
}

// Update is called once per frame
void Update()
{
    IsCoolingDown();

    //if "isInHand" was true do this
    if (pickUpScript.isInHand == true)
    {
        //if mouse left was click and the "weaponIsOverHeat" was false do this
        if (Input.GetMouseButton(0) && !weaponIsOverHeat)
        {
            //if the "weaponheat" was greater than "weaponOverHeat" than do this 
            if (weaponHeat < weaponOverHeat)
            {
                Weapon_Shoot();
                weaponHeat = IsHeating();
            }
        }

        if(Input.GetMouseButtonUp(0))
        {
            print("coolingDown");
            weaponHeat -= 5;
        }
    }
}

void Weapon_Shoot()
{
    RaycastHit hit;

    if (Physics.Raycast(playerCam.transform.position, playerCam.transform.forward, out hit, range))
    {
        EnemyHealth enemy = hit.transform.GetComponent<EnemyHealth>();
        if (enemy != null)
        {
            enemy.TakeDamage(damage);
        }

        if (hit.rigidbody != null)
        {
            hit.rigidbody.AddForce(-hit.normal * impactForce);
        }
    }
}

float IsHeating()
{
    weaponHeat += Random.Range(5f, 9f);

    return weaponHeat;
}

bool IsCoolingDown()
{
    if(weaponHeat >= weaponOverHeat)
    {
        weaponIsOverHeat = true;
    }

    if(weaponHeat < weaponOverHeat)
    {
        weaponIsOverHeat = false;
    }

    return weaponIsOverHeat;
}

the thing that I want to do is when the player not clicking the left mouse the weaponHeat will go decrease overtime but it not working like what I want. I appreciate you help .

Try using a Coroutine to do the cooldown. Something like this pseudo code.

If weapon is used call StartCoroutine(Cooldown()) or when the Player releases the trigger.
that will, once per second, drop the weaponHeat by 5 or a random amount.

IEnumerator Cooldown()
{
   yield return WaitForSeconds(1)  
    if the weaponHeat is > 0 drop by 5.  
    if the weaponHeat is < 0 then set it to 0 and call StopCoroutine(Cooldown())
}

yeah the code run perfectly but I want to stop the coroutine when the weaponHeat reach 0 or the player left click the mouse can you help it here my new code

public class WeaponTest : MonoBehaviour
{
    [Header("Weapon Component")]
    public float damage = 50f;
    public float range = 100f;
    Coroutine isCoolDown;       [Header("Weapon Fire")]

    public float impactForce = 50f;
    public float weaponHeat;
    public float weaponOverHeat = 100f;
    public float timeToCoolDown = 5f;
    private bool fireShoot;
    public bool weaponIsOverHeat;

    public Camera playerCam;
    private PickUpWeapon pickUpScript;

    // Start is called before the first frame update
    void Start()
    {
        //weaponHeat at the start was 0
        weaponHeat = 0;

        //getting pick up script from the player in the active scene
        pickUpScript = GameObject.Find("Player").GetComponent<PickUpWeapon>();
    }

    // Update is called once per frame
    void Update()
    {
        fireShoot = Input.GetMouseButton(0);

        IsCoolingDown();

        //if "isInHand" was true do this
        if (pickUpScript.isInHand == true)
        {
            //if mouse left was click and the "weaponIsOverHeat" was false do this
            if (fireShoot && !weaponIsOverHeat)
            {
                //if the "weaponheat" was greater than "weaponOverHeat" than do this 
                if (weaponHeat < weaponOverHeat)
                {
                    Weapon_Shoot();
                    weaponHeat = IsHeating();
                    StopCoroutine(isCoolDown);
                    Debug.Log("stop cooling down");
                }
            }

            if(!fireShoot)
            {
                isCoolDown = StartCoroutine(IsCoolDown());
            }

            if(weaponHeat <= 0)
            {
              weaponHeat = 0;
            }
        }
    }

    void Weapon_Shoot()
    {
        RaycastHit hit;

        if (Physics.Raycast(playerCam.transform.position, playerCam.transform.forward, out hit, range))
        {
            EnemyHealth enemy = hit.transform.GetComponent<EnemyHealth>();
            if (enemy != null)
            {
                enemy.TakeDamage(damage);
            }

            if (hit.rigidbody != null)
            {
                hit.rigidbody.AddForce(-hit.normal * impactForce);
            }
        }
    }

    float IsHeating()
    {
        weaponHeat += Random.Range(5f, 9f);

        if (weaponHeat > weaponOverHeat)
        {
            weaponHeat = 100;
        }

        return weaponHeat;
    }

    bool IsCoolingDown()
    {
        if(weaponHeat >= weaponOverHeat)
        {
            weaponIsOverHeat = true;
        }

        if(weaponHeat < weaponOverHeat)
        {
            weaponIsOverHeat = false;
        }

        return weaponIsOverHeat;
    }

    IEnumerator IsCoolDown()
    {
        while(!fireShoot)
        {
            yield return new WaitForSeconds(timeToCoolDown);
            if (weaponHeat > 0)
            {
                weaponHeat -= Time.deltaTime;
            }

            if (weaponHeat < 0)
            {
                weaponHeat = 0;
            } 
        }
    }
}