- Home /
 
Fading 3d text - only fades once
Hello all, first off thanks in advance.
But I have looked at how to do this on many forum posts and I think I created the code correctly. The issue is it seems to fade over say 2 seconds once without issue but from then on it will just have a 2 second delay and blink on or off. The goal of the code is to fade in and out at a certain range from the camera.... Seems simple but I have no idea what would cause this to just work once and stop after that... Any help would be greatly appreciated. Thanks again.
Script on 3d text:
 var Range = 30.0;
 var target : Transform;
 var _time:float;
 
 function Update ()
 {
    if (Vector3.Distance(transform.position, target.position) > Range){
        Fade.use.Alpha(GetComponent(TextMesh).renderer.material,1.0,0.0,_time, EaseType.In);
     }
 
 if(Vector3.Distance(transform.position, target.position) < Range){
        Fade.use.Alpha(GetComponent(TextMesh).renderer.material,0.0,1.0,_time, EaseType.Out);
     }
 }
 
               Script on Fade.js:
 function Awake () {
     if (use) {
         Debug.LogWarning("Only one instance of the Fade script in a scene is allowed");
         return;
     }
     use = this;
 }
 
 function Alpha (object : Object, start : float, end : float, timer : float) {
     yield Alpha(object, start, end, timer, EaseType.None);
 }
 
 function Alpha (object : Object, start : float, end : float, timer : float, easeType : EaseType) {
     if (!CheckType(object)) return;
     var t = 0.0;
     var objectType = typeof(object);
     while (t < 1.0) {
         t += Time.deltaTime * (1.0/timer);
         if (objectType == GUITexture)
             (object as GUITexture).color.a = Mathf.Lerp(start, end, Ease(t, easeType)) * .5;
         else
             (object as Material).color.a = Mathf.Lerp(start, end, Ease(t, easeType));
         yield;
     }
 }
 
 function Colors (object : Object, start : Color, end : Color, timer : float) {
     yield Colors(object, start, end, timer, EaseType.None);
 }
 
 function Colors (object : Object, start : Color, end : Color, timer : float, easeType : EaseType) {
     if (!CheckType(object)) return;
     var t = 0.0;
     var objectType = typeof(object);
     while (t < 1.0) {
         t += Time.deltaTime * (1.0/timer);
         if (objectType == GUITexture)
             (object as GUITexture).color = Color.Lerp(start, end, Ease(t, easeType)) * .5;
         else
             (object as Material).color = Color.Lerp(start, end, Ease(t, easeType));
         yield;
     }
 }
 
 function Colors (object : Object, colorRange : Color[], timer : float, repeat : boolean) {
     if (!CheckType(object)) return;
     if (colorRange.Length < 2) {
         Debug.LogError("Error: color array must have at least 2 entries");
         return;
     }
     timer /= colorRange.Length;
     var i = 0;
     var objectType = typeof(object);
     while (true) {
         var t = 0.0;
         while (t < 1.0) {
             t += Time.deltaTime * (1.0/timer);
             if (objectType == GUITexture)
                 (object as GUITexture).color = Color.Lerp(colorRange[i], colorRange[(i+1) % colorRange.Length], t) * .5;
             else
                 (object as Material).color = Color.Lerp(colorRange[i], colorRange[(i+1) % colorRange.Length], t);
             yield;
         }
         i = ++i % colorRange.Length;
         if (!repeat && i == 0) break;
     }   
 }
 
 private function Ease (t : float, easeType : EaseType) : float {
     if (easeType == EaseType.None)
         return t;
     else if (easeType == EaseType.In)
         return Mathf.Lerp(0.0, 1.0, 1.0 - Mathf.Cos(t * Mathf.PI * .5));
     else if (easeType == EaseType.Out)
         return Mathf.Lerp(0.0, 1.0, Mathf.Sin(t * Mathf.PI * .5));
     else
         return Mathf.SmoothStep(0.0, 1.0, t);
 }
 
 private function CheckType (object : Object) : boolean {
     if (typeof(object) != GUITexture && typeof(object) != Material) {
         Debug.LogError("Error: object is a " + typeof(object) + ". It must be a GUITexture or a Material");
         return false;
     }
     return true;
 }
  
 
              Answer by Eric5h5 · Oct 03, 2012 at 04:20 AM
You're starting a new instance of the Fade routine every frame. You should call it once only, until it's finished. Either don't use Update (e.g., use a trigger with OnTriggerEnter/Exit), or else add logic so that it's only called once when needed and not every frame.
Thank you for the answer, I adjusted my code below, now using triggers and a collider box... no luck though, been messing with the options but struggling.. sigh... am I missing something with the code by any chance?
var _time:float;
function OnTriggerEnter (other : Collider)
{
    Fade.use.Alpha(GetComponent(Text$$anonymous$$esh).renderer.material,1.0,0.0,_time);
 
                  
}
function OnTriggerExit (other : Collider)
{
    Fade.use.Alpha(GetComponent(Text$$anonymous$$esh).renderer.material,0.0,1.0,_time);
 
                  }
Yup, Im retarded... and tierd... This did work, just forgot to add a collider to the text. Thanks for the answer!
Your answer
 
             Follow this Question
Related Questions
Fading 3D Text 1 Answer
Fade in 3d Text 2 Answers
Fade 3d text with lerp? 1 Answer