- Home /
Decrease int variable over time
Basically without going into too much detail about my project I'm writing a horror survival/3rd person shooter. (its kind of top secret) lol. I have a flashlight that I got working so far when f is pressed it turns on/off plays a little clicking sound, but now I want to add battery power. Kind of like if flashlight is turned off drain power by 5 every 5 seconds, and what not. when its turned off increase power by 5 every 5 seconds if it goes below 1 disable until full again. etc. I can do the code I'm just a little lost at how I would start with decreasing the power variable if flashlight is enabled over time. the code I have so far is this (its in JS btw)
var flashlight : Light;
var flashlightsound : AudioClip;
var flashlightMaxPower = 100;
var flashlightCurPower = 100;
function Update () {
if (Input.GetKeyDown(KeyCode.F)){
flashlight.enabled = !flashlight.enabled;
audio.clip = flashlightsound;
audio.Play();
}
// Write the code for the battery power
}
any help would be much apreciated.
Answer by save · Jan 13, 2012 at 03:12 PM
Or if you want a more optimized solution than Time.deltaTime, use invokes.
var flashlight : Light; var flashlightMaxPower : int = 100; var flashlightCurPower : int = 100;
function Start () { InvokeRepeating("FlashlightPower", 5, 5); }
function FlashlightPower () { if (flashlight.enabled) { if (flashlightCurPower>0) flashlightCurPower-=5; } else { flashlight.enabled = false; } } else { if (flashlightCurPower<flashlightMaxPower)+=5; } }
Answer by FLASHDENMARK · Jan 13, 2012 at 03:13 PM
It would be easiest if you converted your flashlightCurPower to a float value. By doing so can easily reduce the float by Time.deltaTime, which will result in a decrease of 1 ever seconds. You cannot decrease a integer value with Time.deltaTime since it is of a float value.
var flashlightCurPower : float = 100;
function Update (){
if(flashlight is on){
flashlightCurPower -= Time.deltaTime;
}
flashlightCurPower = Mathf.Clamp(flashlightCurPower, 0 flashlightMaxPower); // Make sure the battery level does not become less than 0 or max than 'flashlightMaxPower'
}
Some thing like that would my way to go.
how would I Add This To $$anonymous$$y Flash Light ($$anonymous$$y One Is Called Flashlight)
Answer by Larry-Dietz · Jan 13, 2012 at 03:09 PM
To give you a point to get started, look at time.deltaTime. It will give you the time that has passed since the last update. If you are wanting to decrement power by 5 every 5 seconds, create a variable to store elapsed time and increment it by time.deltaTime each update, then check to see if it is > 5, if so, decrement your power, and reset your variable back to 0.
Hope this helps, -Larry
Answer by astracat111 · Mar 11, 2018 at 02:55 AM
Use floats and use this:
if (flashlightCurPower > 0)
flashlightCurPower = flashlightCurPower - 5f * (60f * Time.deltaTime);
} else if (flashlightCurPower <= 0) {
flashlight.enabled = false;
}
Why "5 * 60"? You basically just say "300" which would be more natural. Which means you subtract 300 every second.
Your answer
Follow this Question
Related Questions
Delaying a dynamic Variable(transform.position for Ex.) 2 Answers
Creating a variable jump using deltaTime in C#. 0 Answers
timed value increase? 1 Answer
Time variables not interacting 1 Answer
Yielding for a changing amount of time? 2 Answers