- Home /
Start timer with trigger
Hello,
I need to start a timmer when a boolean is true, but I can't make it work. Any help?
Thanks in advance!
#pragma strict
var burn : boolean;
var col : Color = Color.white;
private var timer : float;
function Update() {
timer = Time.deltaTime;
if(burn)
{
ResetTimer();
col = Color.Lerp(Color.white, Color.red, timer);
}
renderer.material.color = col;
}
function ResetTimer() {
timer = 0.0;
}
Can't make it work? Post your script and lets see if we can help.
You have 750+ of karma but is asking something really trivial and propose a script that makes little sense. Am I missing something here?
burn is not set anywhere, ResetTimer does not have any purpose apart from setting timer to 0 which will stop any lerping from happening. As a result, when getting inside the if statement, timer is 0, lerp is not doing.
I guess you meant to use
timer += Time.deltaTime;
Ok so burn comes from somewhere else but let's see your Update:
function Update() {
timer = Time.deltaTime; // Timer is set to deltaTime
if(burn) // if true
{
ResetTimer(); // Timer is set to 0
// Nothing happens since the ratio is 0
col = Color.Lerp(Color.white, Color.red, timer);
}
renderer.material.color = col; // Color does not change
}
Now you could reset timer after the lerp so that the value is applied to the Lerp. But actually the timer = deltaTime, is useless where it is if not use somewhere else.
I just realize as well that your color may not change since you are lerping from white to red with the same ratio.
Here is a review: var timer :float = 0; function Update() { if(burn)
{ timer += Time.deltaTime; renderer.material.color = Color.Lerp( renderer.material.color, Color.red, timer); } }
This code above will lerp from current color to red over 1 second, current probably being white from the start.
Add a multiplier to the timer
timer += Time.deltaTime * 1 / speed;
Answer by Dreamora · Jun 23, 2014 at 08:55 AM
The problem with your code is that you use if(burn) only. If you do not want to restart the timer every frame again (which basing on the code could happen), you would need to have an
if (burn && timer <= 0.0)
To ensure that float inaccuracy does not hit you, I would set the timer to -1 when its reset to clearly mark it as 'not running', but you could instead use a second bool to track the timer being 'running' independent of using its timer value
Your answer
Follow this Question
Related Questions
Time does not start counting down when need to 1 Answer
CountDown Timer Help (Seconds problem) 2 Answers
How to stop a Countdown Timer? 1 Answer
Format String Minutes:Seconds 3 Answers
Stop and Resume Timer 0 Answers