- Home /
smoothly switch between 2 colors that are using lerp
i have a simple script that changes the emission color based on a bool, the first color is its idle color which is a lerp that goes from orange to light purple.
the second color is its "isinDanger" state and this lerps between orange and red, mkis there any way to make the transition between both of these color variants smooth. when the bool is set to true the colors switch instantly with no transition and iv'e tried numerous things to try to get them to be smooth but ultimately it just breaks the lerping.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GemGlowOrange : MonoBehaviour { public Material Mat;
Color Red;
Color lightpurple;
Color orange;
public bool isInDanger = false;
Color CurrentColor; float startTime;
public float speed = 1.0f;
void Start() {
startTime = Time.time;
orange = new Color(1.320755f, 0.5346785f,0.2928088f,1f);
Red = new Color(0.858f, 0f,0f,1f);
lightpurple = new Color(1.320755f,0.2973427f,0.8263685f,1f);
var Mat = GetComponent<Renderer>().sharedMaterial;
Mat.EnableKeyword("_EMISSION");
}
void Update() {
float frac = (Mathf.Sin(Time.time - startTime) * speed);
CurrentColor = Mat.GetColor("_EmissiveColor");
if(!isInDanger){
Mat.SetColor("_EmissiveColor", Color.Lerp(lightpurple, orange, frac));
} else if (isInDanger) {
Mat.SetColor("_EmissiveColor", Color.Lerp(Red, orange, frac));
}
} }
Answer by Headrush95 · Jul 29, 2020 at 11:23 PM
hi zeal i managed to fix this with some help from someone on another forum the code i ended up using was this and it works wonderfully!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GemGlowOrange : MonoBehaviour
{
public Material Mat;
Color Red;
Color lightpurple;
Color orange;
public bool isInDanger = false;
Color CurrentColor;
float startTime;
public float speed = 1.0f;
public float DangerLevel;
void Start() {
startTime = Time.time; // time since game started
orange = new Color(1.320755f, 0.5346785f,0.2928088f,1f); // making my colours RGBA
Red = new Color(0.858f, 0f,0f,1f);
lightpurple = new Color(1.320755f,0.2973427f,0.8263685f,1f);
var Mat = GetComponent<Renderer>().sharedMaterial; // making a reference to the material so we can enable the emission keyword if you do not want this to affect every object using this
//material then use "Getcomponent<Renderer>().material" the same goes with setting the color as you can see further down.
Mat.EnableKeyword("_EMISSION"); // this is making sure this shader has the emission keyword enabled.
}
void Update() {
float TransitionDuration = 0.5f;
DangerLevel = Mathf.MoveTowards(
DangerLevel,
isInDanger ? 0f : 1f,
Time.deltaTime / TransitionDuration);
float frac = (Mathf.Sin(Time.time - startTime) * speed);
Color DangerColor = Color.Lerp(lightpurple,Red, DangerLevel);
Mat.SetColor("_EmissiveColor", Color.Lerp(DangerColor, orange, frac));
}
}
Answer by WaqasHaiderDev · Jul 23, 2020 at 01:06 AM
Hi, Did you try coroutines? I have not tried your code but usually for execution of functions where you want to spread execution over multiple frames instead of completing the function in one frame for what so ever reason, coroutines are used. Below is a document from untiy, which even discussed a case similar to yours. Just read out first few paragraphs and tell if this solved your issue. Coroutines Unity