- Home /
Question by
belikerich · Feb 04, 2021 at 09:38 PM ·
colorcolor changegradient
Apply gradient between two colors on x amount of objects
Hi,
I currently have a game like Stack where each block you place changes color. The effect is some sort of rainbow right now (see image).
Code:
CurrentCube.GetComponent<MeshRenderer>().material.SetColor("_Color", Color.HSVToRGB((Level / 100f) % 1f, 1f, 1f));
But I would like the colors to go from one color to another. For example from blue to green. What is the easiest way to get this working?
screenshot-2021-02-04-at-172431.png
(65.0 kB)
Comment
Answer by unity_ek98vnTRplGj8Q · Feb 04, 2021 at 10:15 PM
https://docs.unity3d.com/ScriptReference/Color.Lerp.html
Use this to define two colors, then you can use the number of the block you are placing to lerp between. So if you wanted to lerp between Red and Green over 100 blocks, you could do something like
color = Color.Lerp(Color.Red, Color.Green, ((float)blockIndex)/100f)