0
\$\begingroup\$
var targetscript : Diamond;
var red : Color;
var orange : Color;

function Start () {
    gameObject.camera.backgroundColor = red;
}

function Update () {
    if (targetscript.score > 4) {
        gameObject.camera.backgroundColor = Color.Lerp(red, orange, Time.time);
    }
}

So right now, if the score is larger than 4 then it would change the camera background color to orange with lerp. But its too quick. I read on the internet that Time.time is 1 second. What is the alternative way but for 2 or 3 seconds? I tried this : http://answers.unity3d.com/questions/328891/controlling-duration-of-colorlerp-in-seconds.html I tried the code for the voted answer but it didn't work. It still lerps through quickly. Does anyone have any ideas? Thanks

\$\endgroup\$
1
\$\begingroup\$

You seem to misunderstand Lerp. The last parameter is the blend-factor (not time) and should be a value from 0 - 1. In your case, a blend-factor of 0 results in red and 1 results in orange.

You can just plug in Time.time, but this value will get truncated to 1 as soon as it's bigger than 1... and that point will be reached 1 second into your game. Doesn't sound like the thing you want to do.

So what you should do is have a "start-time" and a desired duration. Example:

var duration:float = 3; // duration of 3 seconds
var startTime:float = -1;

Then you need to assign the startTime as soon as your color-change starts and use it to calculate the time that has passed. Example:

function Update () {
    if (targetscript.score > 4) {
        if(startTime < 0){
            startTime = Time.time;
        }
        gameObject.camera.backgroundColor = 
            Color.Lerp(red, orange, (Time.time - startTime) / duration);
    }
}
\$\endgroup\$
  • \$\begingroup\$ @mark You may also want to take a look at Time.deltaTime[ Documentation ] which returns the time since the last frame. I find that is usually more intuitive for me. \$\endgroup\$ – Luke Noonan Sep 9 '14 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.