- Home /
Answer by Dave-Carlile · Dec 21, 2012 at 07:23 PM
You need to be able to keep track of the life of the torch. A good way to do this is by using a variable to store the amount of time you want the torch to live.
float life = 20; // life in seconds
In the Update method, subtract off Time.deltaTime to reduce the torch's available life. Update gets called every frame, and Time.deltaTime is the elapsed time since the last frame.
The torch's life is over when life reaches zero, so you then do whatever you need to do to disable the torch - disable light, destroy it, whatever action you need.
If the player picks up some fuel or something, you can add time back to the life variable to keep it going longer.
void Update()
{
life -= Time.deltaTime;
if (life > 0)
{
// TODO : do stuff when the torch is active
}
else
{
// TODO : do stuff when the torch dies
}
}
Tnx! I use JS but got it to work anyway cuz it´s pretty easy code :P
Answer by JaydenM1997111 · Dec 21, 2012 at 09:44 PM
var flashlight : GameObject; //set this as your spotlight
var wait:float = 10; //how many seconds it takes to reduce your light intensity
var reduceby:float = 0.05; //how much it reduces by each time the wait ticks
var seconds:float;
function Update ()
{
seconds += Time.deltaTime;
if(seconds > wait)
{
flashlight.light.intensity -= reduceby;
seconds = 0;
}
}
Hope this helps :)
remember to use a spotlight for your torch light
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Show GUITexture by picking an item. 0 Answers
Now how to get Started? 1 Answer
Ball in Pong dissapears after hitting paddle 0 Answers
How To make game installer ? 3 Answers