- Home /
The question is answered, right answer was accepted
Fade 3d text with lerp?
Hello!
I want the Main Menu 3D Texts to slowly fade out when the user hits play. I used this script but unfortunately it makes the letters black, not transparent.
Any help?
#pragma strict
var buttonState : int = 1;
var click : AudioClip;
var LevelToLoad : String;
var quitButton : GameObject;
var title : GameObject;
var lerpedColor : Color = Color.white;
private var stop : boolean;
function OnMouseEnter () {
if(!stop)
{
renderer.material.color = Color.blue;
audio.PlayOneShot(click);
}
}
function OnMouseExit () {
if(!stop)
{
renderer.material.color = Color.white;
}
}
function OnMouseUp () {
if(buttonState == 1)
{
Camera.main.animation.Play();
stop = true;
//Application.LoadLevel("LevelToLoad");
}
else if(buttonState == 2)
{
Application.Quit();
}
}
function Update () {
if(stop)
{
lerpedColor = Color.Lerp(Color.white, Color.black, Time.time);
renderer.material.color = lerpedColor;
quitButton.renderer.material.color = lerpedColor;
title.renderer.material.color = lerpedColor;
}
}
Have you tried changing it to`lerpedColor = Color.Lerp(Color.white, Color.clear, Time.time);`
You need to keep track of the start time and subtract it from the current time when lerping.
in On$$anonymous$$ouseUp:
stop = true;
startTime = Time.time;
in Update:
if (stop)
{
lerpedColor = Color.Lerp( Color.white, Color.clear, Time.time - startTime );
The Color.clear worked! Thanks! But it disapears the letters immediatly, probably because the time.time starts from the beggining and not from when I click the button... How can I solve that?
Look at my previous post. You need to know the start time.
Answer by fafase · May 29, 2013 at 07:29 PM
Have you tried changing it tolerpedColor = Color.Lerp(Color.white, Color.clear, Time.time);
this is wrong for two reasons, first lerping goes from the current value to the target value, and assign the result to current in order to linearly move (hence the name lerp). What you propose is to get the difference between white and black and assign the difference to the value. Second, your ratio is Time.time which means after 1s you have full move at once. Only during the first second can you see the interpolation.
Here is how you do it:
void Update () {
Color col = renderer.material.color;
col.a = Mathf.Lerp (col.a,0f,0.01f);
renderer.material.color = col;
}
This is greatly simplified and should be put in a function for control, but the idea that you get the color and modified the a for alpha transparency. Lerp will get from the value of a which should be 1 down to 0 with a ratio of 0.01 per frame. As a result you will never really get down to 0 so you would probably favor something like:
void Update(){
ratio += Time.deltaTime;
Color col = renderer.material.color;
col.a = Mathf.Lerp (col.a,0f,ratio);
renderer.material.color = col;
}
This will interpolate over 1second. Adding a multiplier to ratio allows you to control the period of time, use < 1 for increasing time and > 1 to reduce time.
Note you can also use Color.Lerp.
Color is a value type so your way allocates another Color object in order to lerp the alpha and assign, and then reassign to the material. Not entirely sure that's much faster, although I haven't tested. I don't disagree though, it may be an improvement.
Also your ratio variable needs to be set to zero On$$anonymous$$ouseUp. Pretty much a different path to the same solution I provided. But the zero reset is what is actually causing his issue. He's not acknowledging when he started the fade.
This is a C# version, you would not have to do the Color assignment trick in UnityScript but in C#, Unity built-in structs are read only so you have to do this as you cannot modify the a directly.
Since Color is a value type that happens on the stack so no big issue.
As for the ratio, it is not the only issue here as I said this should help but should also be moved to a function, a coroutine would do it nice:
IEnumerate LerpThat(float speed){
float ratio = 0;
while(ratio < 1){
ratio += Time.deltaTime * speed;
Color col = renderer.material.color;
col.a = $$anonymous$$athf.Lerp (col.a,0f,ratio);
renderer.material.color = col;
yield return null;
}
}
Sorry, I wasn't paying careful enough attention to how he was using Color.Lerp
. Obviously, he wasn't Lerping correctly, I just noticed that he wasn't Lerping to the correct end-value.
Follow this Question
Related Questions
How to fade in GUI text. 2 Answers
Easy fade in/out on level load/end? 2 Answers
Fade In and out when level is loaded 2 Answers
Alpha not displaying when changed in code, unless changed manually in the inspector 0 Answers
Fade In/out issue about a gameobject 0 Answers