Hi - I’m trying to trigger and smooth color transition when I hover on a cube, and back to the original color when exiting. You’ll find the code below: it does change the color but ignores the LERP (the intermediate colors). What’s more strange is that the renderer.material.color is properly updated with all lerped values(proven by the debug log), but just not rendered on screen (only until the last value).
Not sure what Im missing.
public Color colorOn; //change in inspector
public Color colorOff;//change in inspector
public float duration; //secs for the transition - change in inpector
float t = 1;
bool turnOn;
bool turnOff;
private Renderer therenderer;
void Start()
{
therenderer = GetComponent<Renderer>();
therenderer.material.color = colorOff;
}
void OnMouseEnter()
{
turnOn= true;
turnOff = false;
Debug.Log("Trun ON!!)");
turnItOn();
}
void OnMouseExit()
{
turnOn = false;
turnOff = true;
Debug.Log("Trun OFF!!)");
turnItOff();
}
void turnItOn() {
while (t > 0 && turnOn)
{
therenderer.material.color = Color.Lerp(colorOff, colorOn, t);
t -= Time.deltaTime / duration;
Debug.Log(therenderer.material.color.ToString());
}
t = 1;
turnOn = false;
}
void turnItOff()
{
while (t > 0 && turnOff)
{
therenderer.material.color = Color.Lerp(colorOn, colorOff, t);
t -= Time.deltaTime / duration;
Debug.Log(therenderer.material.color.ToString());
}
t = 1;
turnOff = false;
}
}
Thanks!