- Home /
lerp between emission colours
hey guys, so i have been trying all day to do this and can't find anything on it anywhere, so i know how to lerp between colors for the main colour of a material, and i know how to set colours in C#, but ive been trying to lerp the emission colour from orange to red, and nothing seems to be working, here is my code;
please help me out here guys, i know i'm gonna look dumb when it turns out to be super simple :').
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GemGlow : MonoBehaviour
{
public Material Mat;
Vector4 red;
Vector4 orange;
void Start() {
orange = new Vector4(1.320755f, 0.5346785f,0.2928088f,0.8182f);
red = new Vector4(0.858f, 0f,0f,0.8182f);
var Mat = GetComponent<Renderer>().material;
Mat.EnableKeyword("_EMISSION");
}
void Update() {
Mat.SetColor("_EmissionColor",(Color.Lerp(orange, red, 1)));
}
}
Answer by Headrush95 · Jul 19, 2020 at 12:12 AM
btw i figured this out for anyone struggling,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GemGlowOrange : MonoBehaviour
{
public Material Mat;
Color Red;
Color red;
Color lightpurple;
Color orange;
public bool isInDanger = false;
float startTime;
public float speed = 1.0f;
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() {
if(!isInDanger){
float frac = (Mathf.Sin(Time.time - startTime) * speed);
GetComponent<Renderer>().sharedMaterial.SetColor("_EmissiveColor", Color.Lerp(lightpurple, orange, frac)); // getting the same material over and over again each frame and updating it's
// emission colour
} else if (isInDanger) {
float frac = (Mathf.Sin(Time.time - startTime) * speed);
GetComponent<Renderer>().sharedMaterial.SetColor("_EmissiveColor", Color.Lerp(Red, orange, frac)); // if it is in danger then it will alternate between red and orange.
}
For me, the code didn't work until I changed "_EmissiveColor" to "_EmissionColor" in line 63 and 75 - I'm not sure, if this was a typo or both things exist, but maybe this information can help someone :)
Answer by ThugsInMugs · Jul 06, 2020 at 04:52 PM
I cant test right now but wouldn't it just be Vector4.Lerp(Color 1 values, color 2 values, speed / Time.deltaTime); ? Lerping is for numerical values.
Your answer
![](https://koobas.hobune.stream/wayback/20220613002622im_/https://answers.unity.com/themes/thub/images/avi.jpg)