Smooth change between color material

Hi all :slight_smile:

For the selection of objects I created a color change (Highlight). Reading the Unity API I discovered the Color.Lerp function and applied it to the reference material for the change between the two colors. It works well but has a very fast interval!

The public float duration = 1.0f; must be set with very low values (eg. 0.07) but has always fast change. I tried various solutions but without success… Do you have suggestions or tips to slow down the flashing?

My script reference:

...
    public Material highLightMat;

    private Color startColor = new Color(255f,255f,255f);
    private Color endColor = new Color(255f, 0f, 0f);
    public float duration = 1.0f;
...
...
    void Update()
    {
        highLightMat.color = Color.Lerp(endColor, startColor, Mathf.PingPong(Time.time, duration));
    }

Thanks for any tip :slight_smile:

Color is 0 to 1, not 0 to 255.

1 Like

What do you mean? As written in API, t is clamped between 0 and 1. Color startColor and Color endtColor are the colors to be used for display. So I think t is the focal point (or I missed a little something?)

Colors are in value range 0-1, 0 being black and 1 white (if every channel had same value.)
So your 255 is way over that. They are not 0-255 8-bits per channel values.

But if you want to use 0-255 range for some reason, you have to normalize your values to that 0-1 range.

1 Like

Ahhh ok, I hadn’t grasped the concept!! But normalizing the color range, does the change speed change or not? (Now I don’t have my pc at hand for tests)

If you want to input your colors as 8-bit you can use Color32, which allows you to set values in 8-bit color range.
But I would really learn to like that 0-1 range. Also so much easier when you work with shaders etc. For example, it’s not that hard to make a green color, which would be (0,1,0) instead of (0,255,0) :).

1 Like

Intrigued, I turned on my work PC and tried…Ok it’s work!
Normalizing the RGB value to 0-1 is perfect. I know the values 0-1 well, but I got confused because I had the color palette at 0-255 and therefore I marked the corresponding colors. Thanks @Olmi and @StarManta for the valuable suggestion. My brain was going in fish fry! If you have any other suggestions I’m here with my ears open!

Thanks!

Edit: So if I understand correctly, the value of t is the color range between 0 and 1? Guys forgive me if I sometimes get lost in a glass of water, but I’m a C# noob :slight_smile:

The value of t is how far between the two colors you want. If A is red, and B is blue:
Color.Lerp(A, B, 0f); //red
Color.Lerp(A, B, 0.5f); //purple
Color.Lerp(A, B, 1f); //blue

1 Like