duck

Unity 3D Javascript appear color fade without coroutine

Feb 7th, 2012
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var colorStart : Color = Color.red;
  2. var colorEnd : Color = Color.green;
  3. var animationDuration : float = 1.0;
  4. private var startTime : float;
  5. private var endTime : float;
  6. private var finished = true;
  7.  
  8. function Update () {
  9.  
  10.     if (!finished) {
  11.    
  12.         var i = Mathf.InverseLerp(startTime, endTime, Time.time);
  13.         renderer.material.color = Color.Lerp (colorStart, colorEnd, i);
  14.         finished = (Time.time > endTime);
  15.                
  16.     } else {
  17.    
  18.         if (renderer.enabled) {
  19.             StartAnimation();
  20.         }
  21.        
  22.     }
  23.  
  24. }
  25.  
  26. function StartAnimation() {
  27.  
  28.     startTime = Time.time;
  29.     endTime = startTime + animationDuration;
  30.     finished = false;
  31.    
  32. }
Advertisement
Add Comment
Please, Sign In to add comment