I am trying to fade the alpha of 3d text over time. I tried color.lerp already, but it is having the exact same issue.
Here is my current code:
public Color fontColor;
public Color newColor;
public float fadeRate = 1;
// Use this for initialization
void Start () {
fontColor = renderer.material.color;
//newColor = new Color(fontColor.r, fontColor.g, fontColor.b, 0);
}
// Update is called once per frame
void Update () {
var alphaValue = Mathf.Lerp(fontColor.a, 0, fadeRate * Time.deltaTime);
newColor = new Color(fontColor.r, fontColor.g, fontColor.b, alphaValue);
renderer.material.color = newColor;
}
}
Any insights appreciated!
Your code says that alpha value will always equal fadeRate * deltaTime. It doesn’t matter how many time Update runs, because it’s always generating the same value (give or take the difference of the length of a frame).
You probably want to use a coroutine, or increase some value as a function of fadeRate * deltaTime. Ex:
var lerpValue : float = 0;
void Update()
{
lerpValue += fadeRate * Time.deltaTime;
if (lerpValue <= 1)
{
// do your lerp
}
}
But that solution has obvious drawbacks. You can only cycle the lerp once, and without additional logic, only in one direction. That’s where a coroutine comes in.
#pragma strict
var lowerLimit : float = 1;
var upperLimit : float = 3;
function Start ()
{
CycleRandomColors();
}
function CycleRandomColors()
{
while (true)
yield ChangeColor(GetRandomColor(), Random.Range(lowerLimit, upperLimit));
}
function ChangeColor(targetValue : Color, duration : float)
{
var startTime : float = Time.time;
var endTime : float = startTime + duration;
var initialValue : Color = renderer.material.color;
var currentValue : Color = initialValue;
var lerpValue : float = 0;
Debug.Log(String.Format("Changing to {0} over {1} seconds.", targetValue, duration));
while (endTime >= Time.time)
{
lerpValue += Time.deltaTime / duration;
renderer.material.color = Color.Lerp(initialValue, targetValue, lerpValue);
yield;
}
}
function GetRandomColor() : Color
{
return new Color(Random.value, Random.value, Random.value, Random.value);
}