- Home /
Why change the light color so fast?
Under script is my question.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class LightColorChange : MonoBehaviour {
private Color kleuR;
public GameObject dirLight;
public float takeColorTime = 30;
void Update () {
kleuR =Color.Lerp(Color.red, Color.blue , takeColorTime * Time.deltaTime);
}
void FixedUpdate() {
dirLight.GetComponent<Light> ().color = kleuR ;
}
}
why is my color chaning so fast and how an i fix it?
Answer by Priyanka-Rajwanshi · Apr 24, 2018 at 02:24 PM
@meenjedat Color.Lerp has the third parameter that interpolates betewen 0 and 1 and you are giving it a large value. What you can do is that take a variable and increment it in Update with Time.deltaTime and make takeColorTime value to be small.
float interval = 0f;
void Update () {
interval += Time.deltaTime;
kleuR =Color.Lerp(Color.red, Color.blue , takeColorTime*interval);
}
edit: didn't see this answer. $$anonymous$$oving $$anonymous$$e to comment.
@meenjedat : Unless you're writing a script that cleverly changes your light based on how long frame calculations take, this isn't going to be what you want.
Here, you are always starting between Red and Blue -- there will not be any transition at all because you aren't keeping track of intermediate steps along your supposed transition.
Your time multiple also looks very high for lerp. Is this what you meant to do?
private float colorTransition$$anonymous$$ultiple = 1;
private float colorTransitionProgress = 0;
void Update () {
this.colorTransitionProgress += Time.deltaTime * this.colorTransition$$anonymous$$ultiple ;
kleuR =Color.Lerp(Color.red, Color.blue , this.colorTransitionProgress);
}
void FixedUpdate() {
dirLight.GetComponent<Light> ().color = kleuR ;
}
TreyH your method is working, Thanks. But it is still fast not a slow wa to let it change slowly color???
Reduce your multiple:
private float colorTransition$$anonymous$$ultiple = 0.1f;
If you are this new to C#, you may want to read up on some guides before continuing with Unity.
Your answer
Follow this Question
Related Questions
How to change the color from code 1 Answer
Light Looks Weird 2 Answers
Changing light color back and forth 1 Answer
Change fade distance of shadows on pointlight through script in HDPR 0 Answers
Get "on screen" color of a texture to use in a legend 2 Answers