- Home /
Lerping 2 values on a shader on button press.
Hello there, I'm having a little trouble with Lerping 2 values on a shader.
The effect I'd like to happen is that when a button is pressed, the object illuminates instantly and then fades down to dark
I've set up 2 public variables which act as the 2 colours I want to Lerp between (bright and notBright) and added them to a variable that is active when "T" is pressed. The variable they are added to then fills in the value for the colour of the shader "_Emission". The Lerp time is set with objBrightSpeed. I've added a boolean to check whether it happens and return a true/false so it only happens once.
The script is throwing up no errors but the object colour starts off as intended and then illuminates on press but fails to lerp to the darker colour, it just stays lit constantly from then on.
Here's what I've scripted for it:
public float objBrightSpeed = 0.2f; // This determines the speed at which it de-illuminates
public bool block01lBrightLerp = false;
public Color block01lBrightness; // This is the Color variable that is the object colour.
public Color block01lBright = new Color (1.0f, 1.0f, 1.0f, 1.0f); // This adds illumination to an object
public Color block01lNotBright = new Color (0.0f, 0.0f, 0.0f, 0.0f); // This removes illumination from an object
void Update ()
{
if (block01lBrightLerp == false) // Makes sure that this function only calls once
{
block01l.renderer.material.SetColor("_Emission", block01lBrightness);
block01lBrightness = Color.Lerp(block01lBright, block01lNotBright, Time.deltaTime * objBrightSpeed);
block01lBrightLerp = true;
}
}
if (Input.GetKeyUp(KeyCode.T))
{
block01lBrightLerp = false;
}
}
Edited as one of the variables didn't seem to show in the post
if anyone could give me a pointer with this it'd be much appreciated!
I'm not sure what the rest of your update function looks like, but there are a couple things I'd like to point out.
First, Lerp takes a value between 0 and 1 as it's third parameter, returning some value between the first and second parameter depending on what that is. It looks like you're passing a value that doesn't ever increase to Lerp - right now you'd always get whatever Time.deltaTime objBrightSpeed happened to be, where ins$$anonymous$$d you probably meant to store a progress variable that increments from 0 to 1 BY Time.deltaTime objBrightSpeed and use that as your third parameter, ensuring a smooth transition from 0 to 1 (and thus from block01lBright to block01lNotBright).
Secondly, from what I can see right now, it looks like you have the brightness set to change only when block01lBrightLerp is false, but are changing it to be true immediately upon detecting that it is false. I can't see the rest of your update function, but this probably means you're only changing the value of block01lBrightness once per release of the "T" key (where you set the lerp boolean to true).
Both of these would prevent you from achieving the smooth transition you're looking for.
Hi LunaArgenteus, thanks for responding.
I'm still very much a newcomer to C# so forgive me if I anything I write seems obvious/stupid.
In the first part you mention about a stored progress variable - is this the part that now reads 'public float objBrightSpeed = 0.2f;' I've edited the post to show that as for some reason it had split my code in 2 and chopped that line off.
In response to the second part, this is all of the update function related to this. $$anonymous$$y aim was that on the press of T it instantly changes the colour to bright and then lerps back to notbright slowly, regardless of whether the button is held or not.
In regards to my thinking - it was that having it start at false means it would allow that lerp to happen, setting it to true once it's happened would mean that it wouldn't keep updating. Having it set to false on key Up would then allow the update to be called again if T is pressed again.
Regarding smooth transition - I intended it to be instant on the press and then smooth on the way out. It's meant to be able to simulate the hitting of a piano key if that helps to put it into a bit of context.
Your answer
Follow this Question
Related Questions
Color.lerp not working properly 2 Answers
lerp transparency back and forth, while cycling through rainbow? 0 Answers
Color.Lerp with Particles/Additive shader 1 Answer
Material doesn't have a color property '_Color' 4 Answers
Color.Lerp doesn't work 1 Answer