I’m scripting in C# and having an issue with a color lerp. I set up an object to fade out with Time.deltaTime (I wanted the user to be able to set the fadeDuration in seconds, for example, 5), it fades out smoothly, but much too quickly, and when I check the fadeTimer, it’s still crawling up at a tiny fraction of 1, which doesn’t seem to make sense. Does anyone know how I can make the duration more accurate? I want to destroy this thing when the timer reaches > 1 which isn’t happening with this code I’ve got.
void Update ()
{
if (startFade) // fade enabled
{
print (fadeTimer);
if (fadeTimer < 1)
{
renderer.material.color = Color.Lerp (renderer.material.color, Color.clear, fadeTimer);
fadeTimer += Time.deltaTime / fadeDuration; // advance timer by fractions
}
else
{
Destroy (gameObject); // once lerp is done (timer = 1), delete object
}
}
}
Works fine with me actually.
– fafase