I’m trying to create a pulsing effect using color.lerp. I can get the colors to change in one direction fine using this cript-
var startColor : Color;
var endColor : Color;
var duration: float = 5; // duration in seconds
5. private var t: float = 0; // lerp control variable
function Update() {
renderer.material.color = Color.Lerp(startColor, endColor, t);
if (t < 1){ // while t below the end limit...
10. // increment it at the desired rate every update:
t += Time.deltaTime/duration;
}
}
But since then I’ve tried to advance the script so the colors will have a pulse effect. However the colors are just flickering at a very fast rate. What in my script is wrong?
#pragma strict
var startColor : Color;
var endColor : Color;
var duration: float = 5; // duration in seconds
var pulselow : boolean = true;
private var t: float = 0; // lerp control variable
function Update()
{
if (pulselow == true)
{
renderer.material.color = Color.Lerp(startColor, endColor, t);
if (t < 1)
{
t += Time.deltaTime/duration;
pulselow = false;
}
}
else
if (pulselow == false)
{
renderer.material.color = Color.Lerp(endColor, startColor, t);
if (t < 1)
{
t += Time.deltaTime/duration;
pulselow = true;
}
}
}
Any suggestions? (make all phrasing easy to understand please…lol)
Thanks
You never reset t, and you switch pulselow at the wrong time. You switch Lerp direction every time t is under 1, so basically every frame during the first 5 seconds.
function Update()
{
t += Time.deltaTime/duration;
if (pulselow) // pulselow is true
{
renderer.material.color = Color.Lerp(startColor, endColor, t);
}
else // no need for another "if". pulselow was not true so it can't be anything else but false
{
renderer.material.color = Color.Lerp(endColor, startColor, t);
}
if (t > 1)
{
t = 0;
pulselow = !pulselow; // negate pulselow => toggle it from true to flase or false to true
}
}