Limited Energy Regeneration

I’m making a Space Sim, and I have a boost script. The problem is, the player can boost for forever so I need to make it so that the boost can only be limited, and the boost starts to regenerate over time when the player isn’t using boost. How should I do this with the existing script:

var Speed : int = 50;
var BoostSpeed : int = 200;
var NormalSpeed : int = 50;   ///Must be same as Speed int

function Update() 
{
    transform.Translate(Vector3.forward * Time.deltaTime * Speed);

    if (Input.GetKeyDown ("space")){
    Speed = BoostSpeed;
}
	if (Input.GetKeyDown ("space")){
BoostSpeed = Speed;
}
	if (Input.GetKeyUp ("space")){
Speed = NormalSpeed;
}
	if (Input.GetKeyUp ("space")){
NormalSpeed = Speed;
}
}

I pretty much figured this out a while ago, but so this has an answer, here’s the script with the limited energy:

var starshipEnergy : float = 500;
var energyLossRate : float = 100;
var energyRegenerationRate : float = 25;
var canBoost : boolean = true;
var boostSpeed : float = 200.0; 
var cruisingSpeed : float = 50.0;
private var Speed : float = 50.0;
private var fighter : boolean = false;

transform.Translate(Vector3.forward * Time.deltaTime * currentSpeed);
 	
 
 	if(!fighter){
 	energyLossRate = 0;
 	energyRegenerationRate = 0;
 	}
 
 	if(Input.GetKey("space") && starshipEnergy > 0 && canBoost){
 	starshipEnergy -= energyLossRate * Time.deltaTime;		
 	}
 	else if(starshipEnergy <= 500 && canBoost){
 	starshipEnergy += energyRegenerationRate * Time.deltaTime;
 	}
 	if(Input.GetKey("space") && starshipEnergy > 0 && canBoost)
 	Speed = boostSpeed;
 	else
 	Speed = cruisingSpeed;

Hope this helps anyone with the same problem. You might need to change some variables to suit your own needs.

Hi, try to use this:

var normalSpeed : float;
var turboSpeed : float;
var limitTurboTime : float;
var maxturbo : float;
var regen : boolean;

function Awake () {
    limitTurboTime = maxTurbo;
}

function Update () {
    If (Input.GetKeyDown("space")) {
        normalSpeed = normalSpeed * turboSpeed;
        regen = false;
        if (limitTurboTime > 0)
            limitTurboTime -= Time.deltaTime;
    }
    if (Input.GetKeyUp("space")) {
        normalSpeed = normalSpeed/turboSpeed;
        regen = true;
    }
    if (limitTurbotime < maxTurbo && regen)
        limitTurboTime += Time.deltaTime;
}

I think this should do the work.

One way to achieve this would be to add some new variables:

var CanBoost : boolean = true; // true or false can we boost rignt now?
var BoostAmount : float = 1; // (0 to 1) amount of boost left
var BoostDrainRate : float = 5; // seconds it should take for a full boost to drain
var BoostRechargeRate : float = 10; // seconds it should take to recharge fully from no boost

then instead of using using GetKeyDown and GetKeyUp you can set the speed for that frame based on whether or not the boost key is pressed. You’ll also probably want to use float instead of int not gonna get into why here but in this case float is probably closer to how you were thinking it should work.

Currently you are moving the GameObject before you update it’s speed. This means that you’re always using the previous frame’s Speed value

var Speed : float = 0; // starts at 0 but it's ok because we set it before we use it
var BoostSpeed : float = 2;
var NormalSpeed : float = 1;

var CanBoost : boolean = true; // true or false can we boost rignt now?
var BoostAmount : float = 1; // (0 to 1) amount of boost left
var BoostDrainRate : float = 5; // seconds it should take for a full boost to drain
var BoostRechargeRate : float = 10; // seconds it should take to recharge fully from no boost

function Update () {
    /* 
     * first decide which speed to use 
     */
    if (CanBoost && Input.GetKey ("z")) {
        BoostAmount = Mathf.Max(0, BoostAmount - Time.deltaTime / BoostDrainRate);
        if (BoostAmount == 0)
        	CanBoost = false; // we ran out of juice stop boosting
        Speed = BoostSpeed;
    } else {
        BoostAmount = Mathf.Min(1, BoostAmount + Time.deltaTime / BoostRechargeRate);
        if (BoostAmount == 1)
        	CanBoost = true; // we're full we can boost again!
        Speed = NormalSpeed;
    }
     
    /*
     * then move 
     */
    transform.Translate(transform.forward * Speed * Time.deltaTime);
}

i also noticed in your code you have multiple if statements for the same condition, you can combine them but in this case the statements you had inside the conditionals were redundant/unnecessary