C# Weapon overheat Buildup/Cooldown

Hallo dear community.
I have a value that increases with 2 different type of shots (small/big) filling my overheat.
The code ads those values and decreases them just as they are supposed.
However because I use Time.time (same problem with Time.deltaTime) the subtracted intervals are not even.
This is important because see awesome art work =P.
50315-problem.jpg

How can I fix this problem or what method could I use else to achieve the subtract 1 every second.

This is how far I got myself:

using UnityEngine;
using System.Collections;

public class FirePlayerCanon : MonoBehaviour

{

public GameObject smallLaser;
public GameObject bigLaser;
public float smallShot = 2.1f;
public float bigShot = 4f;
public float overheat;

public bool heating =false;
public float subtractionDelay = 1f;
public float timeTillNextSubtraction; 
private float minOverheat = 0f;
private float maxOverheat = 7f;




// Update is called once per frame
void Update () 
	
	
	
{
	//fire normal with out delay.
	
	var wasAltHit = Input.GetKeyDown(KeyCode.LeftAlt);
	
	var wasCtrlHit = Input.GetKeyDown(KeyCode.LeftControl);
	
	
	// instatiate smallShot.
	if (wasCtrlHit && heating == true)
		
	{
		Instantiate(smallLaser,transform.position,Quaternion.identity);
		
		//add smallShot valeu to overheat.
		overheat = overheat += smallShot;
	}
	
	// instatiate bigShot.
	if (wasAltHit && heating == true)
		
	{
		Instantiate (bigLaser, transform.position, Quaternion.identity);
		
		//add smallShot valeu to overheat.
		overheat = overheat += bigShot;
	}
	
	//Set max overheated value witch shall not be passed
	if (overheat >= maxOverheat)
		
	{
		overheat = maxOverheat;
		heating = false;
	}
	
	
	//Set min overheated value witch shall not be passed
	if (overheat <= minOverheat)
		
	{
		overheat = minOverheat;
		heating = true;

	}
	



	if (Time.time > timeTillNextSubtraction)
	{
		overheat = overheat -= 1f;
		timeTillNextSubtraction = Time.time + subtractionDelay;
	}

}

}

I am an absolute beginner in terms of coding and would appreciate an answer with an example on how to use. Unity Documentation is not always a 100% clear to me in terms of usage in combination with other code.
Any help at all is much appreciated.

Regards

Chris

Hello agito1987

to make the 2 second delay you can make a bool variable that indicates that the “weapon” is overheating and the overheat should not go down, lets call it Heating. now set it to true everytime you overheat. then you can create a coroutine and check if Heating is set to true and if it is wait for 2 seconds and set it back to false and gradually decrease the heat amount

if you think i am not clear or want more help, i’ll be happy to help

PS: there are some errors in your code.

<[—EDIT—]>
Heres a “Demo” script if i was not clear enough :slight_smile:

public class Demo
{
    int shot, check, value, overheat, minHeat, maxHeat, decVal, incVal;
    bool heating;
    void Update()
    {
        // instatiate Shot
        if (shot == check)//here
        {
            //shooting code here
            overheat += incVal; //increment heat
            heating = true; //heat check
        }

        //heat clamps
        if (overheat >= maxHeat)
        {
            overheat = maxHeat;
            heating = false;
        }
        else if (overheat <= minHeat)
        {
            overheat = minHeat;
            heating = true;
        }


        //now here we decrement
        if (heating)
        {
            StartCoroutine("decHeat");
        }
    }

    IEnumerator decHeat()
    {
        //wait for 1 sec
        yield return new WaitForSeconds(1);
        overheat -= decVal;// decrease the value from overheat
        
    }
}

Regards Faizan