- Home /
Attempting to fade 3d text
Firstly: I know this has been documented, and answered before, as evidenced here among numerous other places. The issue isn't a lack of help, it's my complete newness at coding. I'm attempting to code a fade in and fade out for 3d text. I've placed the code from here into an empty game object in my scene, and am attempting to attach the following code into the 3d text:
Fade.use.Alpha(GetComponent(TextMesh).renderer.material, 0.0, 1.0, 2.0);
{
function ()
{
for(i=1;i>0;i++)
{
yield WaitForSeconds(3.0);
}
}
Entering into this, I knew I would code something wrong, as my entire coding education so far has been stumbling along until something works, and then asking for help, and hoping that the advice not only fixes the current issue, but that I retain why the advise worked. So I guess I'm asking if someone can tell this noob what I need to add to my script to actually make it, you know, a script. Thanks, and God bless.
Answer by clunk47 · Sep 06, 2013 at 07:42 PM
Here's an example I came up with for your question. Simply attach this to your 3DText Object, then use the 'E' key to toggle the fade. I made this only fade out if alpha is 1.0, and only fade in if alpha is 0.0. There are many different ways of doing this but hopefully this will get you going.
Here's a C# Example
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
bool fadeIn = false;
bool fadeOut = false;
float fadeSpeed = 0.01f;
float minAlpha = 0.0f;
float maxAlpha = 1.0f;
Color color;
void Awake()
{
color = renderer.material.color;
}
void Update()
{
renderer.material.color = color;
if(fadeIn && !fadeOut)
FadeIn ();
if(fadeOut && !fadeIn)
FadeOut ();
if(color.a <= minAlpha)
{
fadeOut = false;
if(Input.GetKeyDown (KeyCode.E))
{
fadeIn = true;
}
}
if(color.a >= maxAlpha)
{
fadeIn = false;
if(Input.GetKeyDown (KeyCode.E))
{
fadeOut = true;
}
}
}
void FadeIn()
{
color.a += fadeSpeed;
}
void FadeOut()
{
color.a -= fadeSpeed;
}
}
And here is a Javascript example of the same thing.
#pragma strict
var fadeIn : boolean;
var fadeOut : boolean;
var fadeSpeed : float = 0.01;
var minAlpha : float = 0.0;
var maxAlpha : float = 1.0;
function Update()
{
if(fadeIn && !fadeOut)
FadeIn ();
if(fadeOut && !fadeIn)
FadeOut ();
if(renderer.material.color.a <= minAlpha)
{
fadeOut = false;
if(Input.GetKeyDown (KeyCode.E))
{
fadeIn = true;
}
}
if(renderer.material.color.a >= maxAlpha)
{
fadeIn = false;
if(Input.GetKeyDown (KeyCode.E))
{
fadeOut = true;
}
}
}
function FadeIn()
{
renderer.material.color.a += fadeSpeed;
}
function FadeOut()
{
renderer.material.color.a -= fadeSpeed;
}
You did create a C-Sharp script, and not a JavaScript right?
no worries, lol. I had just been used to using js, but see no reason not to try and learn on either. Although oddly enough, while I get no compile errors, and the script shows up in my assets, it doesn't show up as an available component to add. Sorry, I know this is all stuff that is probably so basic.
Other question is this: if I simply want this to fade in when the player gets in range, and then fade out on its own (it's hovering text over an enemy, think like the boss fights in Ocarina of Time) would I just remove the "get$$anonymous$$eyDown - E" sections?
Edit: You are absolutely incredible. Thank you! This definitely helped me a ton. Next steps for me are trying to get the text to fade without the key press, and incorporating a pause before the battle begins. Thank you SOOOOO much for getting me to this point :)
Yes Yes Yes!!!!!!! Praise God! =) Using Clunk's fantastic js script, and principles learned through Jesse Etzler's tutorials, I managed to get the text to appear when the player is a certain distance, and then fade out again at a closer distance. Here's the modified script: (ps: sorry for double posting, didn't seem to fit the above comment)
#pragma strict
var fadeIn : boolean;
var fadeOut : boolean;
var fadeSpeed : float = 0.01;
var $$anonymous$$Alpha : float = 0.0;
var maxAlpha : float = 1.0;
var $$anonymous$$Distance = 30;
var target : Transform;
var myTransform : Transform;
function Awake()
{
myTransform = transform;
}
function Start()
{
target = GameObject.FindWithTag("Player").transform;
}
function Update()
{
if(fadeIn && !fadeOut)
FadeIn ();
if(fadeOut && !fadeIn)
FadeOut ();
if(renderer.material.color.a <= $$anonymous$$Alpha)
{
fadeOut = false;
if(Vector3.Distance(myTransform.position, target.position) > $$anonymous$$Distance)
{
fadeIn = true;
}
}
if(renderer.material.color.a >= maxAlpha)
{
fadeIn = false;
if(Vector3.Distance(myTransform.position, target.position) < $$anonymous$$Distance)
{
fadeOut = true;
}
}
}
function FadeIn()
{
renderer.material.color.a += fadeSpeed;
}
function FadeOut()
{
renderer.material.color.a -= fadeSpeed;
}
Looks like you're learning fast, I'm always happy to help someone so dedicated to beco$$anonymous$$g a good coder. Happy Developing Sir :D
thanks! Did find a hiccup though: text has actually appeared far earlier than I'd like, plus is showing through walls, buildings, you name it. Definitely fades when I get close, but have to figure out how to not have it show up so soon, since it's the boss' text, and would kind of give away its position :)
EDIT: Hmm, well, attempt 1 at that edit didn't work. $$anonymous$$y theory was to make a new variable: "$$anonymous$$Distance2" and set it up a bit higher than $$anonymous$$Distance, then copy paste the if functions, changing their values. No dice, the text will fade out, but is still always there. So the question is how to make it not fade in until X distance away. Back to the lab again.