How can I change a line renderer’s color over time? I’m trying to use Color.Lerp but I’m not getting it.
public Color red;
public Color white;
private LineRenderer line1;
private float t;
void Start()
{
//Assigned line renderer here
}
void Update()
{
//Raycast code to draw line here
t += Time.deltaTime;
line1.material.color = Color.Lerp(red, white, t);
}
EDIT: I’m gonna change and remove what I wrote here: the stuff about copying the material was completely incorrect, I was thinking about some other function that makes material copies.
I believe your problem may be that the “t” argument in Lerp only goes from 0.0 to 1.0, so after 1 second of time, this will only return white.
Did you know about the Mathf.PingPong() function? You can use it to cycle a number back and forth between two limits.
I’ve seen it in other threads, but I don’t want the value to move back and forth. I want the color to gradually turn white and stay white until reassigned elsewhere/later. But right now, the color doesn’t change at all. I’m wondering if assigning the line material in the editor vs in script affects anything?
I’m not sure what might be going wrong, but I just took a look at the LineRenderer in a more-recent version of Unity and it has a lot of new properties compared with the old version I’m used to.
One of those new-to-me properties is an actual color gradient property, on the actual LineRenderer itself, not the material. What is weird is that my tinkering with that color (the one on the LineRenderer) does not seem to do anything, either with the standard material or with others. Maybe someone else here with more LR experience can chime in?
However, setting the .color on the .material still works as expected: my colors are cycling. I have attached my example codelet and scene.
Update: I think I figured out at least how to get the color gradient working. In this example code:
They use the Default/Sprite material… I guess that respects however the colors are transferred from the LR gradient to the geometry. I thought it was via vertex colors but the underlying geometry (visible by viewing Wireframe in the scene) does not support this assumption. There must be some other sprite gradient mechanism I am unaware of. Again perhaps someone else can chime in?
Correction to the above: I guess it DOES use vertex coloring, but it is up to you to add enough intermediate points to achieve the required gradienting. See this:
Now how all that fits into the basic material NOT working for you, I don’t know. Are you using a Material that respects that .color channel? Not all shaders use it…