- Home /
Color lerp once?
renderer.material.color = Color.Lerp (colorStart, colorEnd, 5*Time.deltaTime);
when I use this code, instead of fading from color 1 to 2 over (x) time, it blinks and never fully changes color?
What am I doing wrong/missing? Thanks!
Answer by almo · Jul 27, 2011 at 09:17 PM
Lerp's third argument is a number from 0 to 1 (inclusive) that indicates how close to the second argument it should go from the first.
If you want it to go completely to the colorEnd, you need to do something like
renderer.material.color = Color.Lerp(renderer.material.color, colorEnd, 0.1);
This will move the color of the material from where it is the first time you call it 10% of the remaining distance toward colorEnd each call.
Your mistake was restarting from colorStart each time it gets called.
Answer by Rispat-Momit · Jul 09, 2017 at 08:02 PM
Here is a simple script for everyone to use ;)
var colorEnd: Color;
var Duration: float = 0.1;
function Update () {
gameObject.GetComponent(Renderer).material.color = Color.Lerp(gameObject.GetComponent(Renderer).material.color, colorEnd, Duration);
}
Except it's bad code that's framerate-dependent, with poorly-named variables. Nobody should use this, sorry.