Ok so that is my script, at the moment I have a spotlight attached to my character and when I press 'Q' it toggles on and off. What I want is to make it have a power source that will run out after a set amount of time, two minutes for example.
As you can see I have set the charge of my spotlight to 100 and it turns on only if the charge is more than 0 however despite trying InvokeRepeating and yield functions to minus 1 from charge every x seconds I could not find a solution.
Any help you can give me to show me how to slowly decrease the charge would be much appreciated (I will also need to increase it again but i can probably work that out myself with a script for decreasing the charge)
Let there be a max charge value such that Charge is between 0 and MaxCharge.
Math.Lerp will give your light a gradient falloff between LightOff and LightOn.
var Charge = 100;
var LightOn = 1;
var LightOff = 0;
private var isLightOn = false;
function Update () {
Charge -= 1 * Time.deltaTime;// you can change 1 to any number to drain the charge over a shorter amt of time.
if(Input.GetKeyDown(KeyCode.Q))
{
isLightOn = !isLightOn;
}
if(isLightOn == true)
{
if(Charge > 0)
{
light.intensity = LightOn;
} } else{
light.intensity = LightOff; }
}