- Home /
Fading 3D Text
I've written this script here to make 3D text display the amount of damage dealt, rise up, then fade out. Since I don't want to have to Instantiate this 3D text over and over again, I'm just trying to re-use the same ones. It works at first, and fades out. For some reason though, once the text is Reset, it no longer fades out. And it looks all weird and blocky when the game starts, and it creates an instance of the the material.
public float fadeDelay = 1;
public float fadeRate = 10;
public float floatVelocity = 3;
private Transform parentD;
private float xVel;
private float zVel;
public bool fading = true;
public Color newColor;
private Transform display;
private TextMesh tM;
void Awake () {
display = transform;
newColor = display.renderer.material.color;
parentD = display.parent;
tM = display.GetComponent<TextMesh>();
}
void Update () {
if(fading){
Vector3 floatDir = new Vector3(xVel, floatVelocity, zVel);
display.Translate(floatDir * Time.deltaTime);
StartCoroutine(Fade());
}
}
IEnumerator Fade () {
yield return new WaitForSeconds(fadeDelay);
newColor.a -= fadeRate * Time.deltaTime;
display.renderer.material.color = newColor;
if(display.renderer.material.color.a <= 0){
display.renderer.enabled = false;
fading = false;
parentD.SendMessage("AddDisplay", display);
}
}
void Reset () {
if(fading) return;
display.renderer.enabled = true;
newColor.a = 100;
if(Random.value > 0.5F){
xVel = -floatVelocity;
}else{
xVel = floatVelocity;
}
if(Random.value > 0.5F){
zVel = -floatVelocity;
}else{
zVel = floatVelocity;
}
fading = true;
}
Not sure whats going wrong here. Reset() is called by the parent, which is what's re-using the 3D text. The overall effect is that when someone attacks someone else, and does damage, a red floating number emits from the other person displaying how much damage was dealt. I've got most of it, just not the resetting. How should I fix this?
Answer by Meltdown · Aug 07, 2012 at 06:03 PM
RGBA values in Unity are normalized. Setting newColor.a = 100;
is an invalid value. It's range is only from 0 - 1.
So you may want to set it to 1 to make sure it shows correctly on reset.
Oh you're right! Yes, by setting to 100, it was fading out, but since it was at 100 it made no difference until a became less than 1... and it didn't even have that long to fade that much. Thanks!
Your answer
Follow this Question
Related Questions
Material doesn't have property '_Color' 4 Answers
Fading multiple objects 0 Answers
Reset my (public)integers on GUI? 0 Answers
How to make 3D text fade with OnMouseEnter? 1 Answer
Gun Barrel To Get Hot - Change _Tint 1 Answer