Quick change over time math

Hello, all! I am writing a script that allows you to set parameters for the time you would like a guitexture to stay dormant, then fade in, then stay constant, then fade out.

I would just like to know how to calculate this:

After the number of dormant seconds has been reached, I would like to calculate the rate at which an object needs to fade in and how to make a fluid transition independent of frames.

For instance, if the FadeinTime variable is set to 4.0 seconds, I would like to take the guitexture’s alpha from 0 to 100 in 4.0 seconds, but instead of updating every second, I’d like to update it as quickly as possible at a set rate.

How would I do this?

Thanks for the help! (And I will release the script when I finish) - YA

You should Lerp the colour in Update().

See Unity’s example here:

You will need to provide your own time value based on simple maths for how long has elapsed since the fade began.

Note you could also do this with a Coroutine, which is probably a better choice, so you don’t Update() unnecessarily:

http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html

Something like:

IEnumerator Fade(material mat, float duration, Color start, Color end)
{  // remember the start
   float start = Time.time;
   do
   {  // calculate how far through we are
      float elapsed = Time.time - start;
      float normalisedTime = Mathf.Clamp(elapsed / duration, 0, 1);
      mat.color = Color.Lerp(start, end, normalisedTime);
      // wait for the next frame
      yield return null;
   }
   while(elapsed < duration);
}

I’ve not compiled it, but it should be fine.