I am making a gameobject to 'pulse' its color , in UNITY 3d .
i want the color to keep shifting between 2 colors ,
following is my code , it doesnt work (i am doing this in the update function) :
renderer.material.color = Color.Lerp(renderer.material.color, TargetColor,fadeSpeed*Time.deltaTime);
if(renderer.material.color == TargetColor)
{
if(renderer.material.color == Color.gray)
{
TargetColor = Color.white;
}
else if (renderer.material.color == Color.white)
{
TargetColor = Color.gray;
}
}
i'm new to this. What am i doing wrong ?
Your code is working if you select a proper value for fadeSpeed like 10. (And don't forget to initialize TargetColor like void Start() {TargetColor = Color.gray;}
)
You are using Lerp in a wrong way. Which causes the color to reach it's target value in a long shot. Add this debug log to somewhere in Update and you can see the problem:
Debug.Log("Current color: " + renderer.material.color);
If I need to code it myself, I would do something like this:
public class ColorBlinker : MonoBehaviour
{
public float FadeDuration = 1f;
public Color Color1 = Color.gray;
public Color Color2 = Color.white;
private Color startColor;
private Color endColor;
private float lastColorChangeTime;
private Material material;
void Start()
{
material = GetComponent<Renderer>().material;
startColor = Color1;
endColor = Color2;
}
void Update()
{
var ratio = (Time.time - lastColorChangeTime) / FadeDuration;
ratio = Mathf.Clamp01(ratio);
material.color = Color.Lerp(startColor, endColor, ratio);
//material.color = Color.Lerp(startColor, endColor, Mathf.Sqrt(ratio)); // A cool effect
//material.color = Color.Lerp(startColor, endColor, ratio * ratio); // Another cool effect
if (ratio == 1f)
{
lastColorChangeTime = Time.time;
// Switch colors
var temp = startColor;
startColor = endColor;
endColor = temp;
}
}
}
Additionally, take a look at tweening libraries. They are very helpful.
http://dotween.demigiant.com/
http://itween.pixelplacement.com/
http://u3d.as/content/dented-pixel/lean-tween/31i
Building on Can's answer, I've used the Sin function to tighten things up a bit. Math.Sin(Time.time) will give you a value that bounces between -1 and 1, then Math.Abs will restrict it to 0-1. (This gives the pulse an easeOut, easeIn type of pulse effect) You can multiply the Time.time value to adjust the speed. *0.5f = half-speed, *2.0f = double-speed, etc.
Just call setHighlight(true/false) to turn it off and on.
public class prop_highlight : MonoBehaviour {
public Color highlight_color;
private bool is_highlighted = false;
private Material material;
void Start() {
material = GetComponent<Renderer>().material;
}
public void setHighlight(bool set_highlight) {
is_highlighted = set_highlight;
if(!set_highlight) material.color = Color.white;
}
void Update() {
if(is_highlighted) {
var ratio = Mathf.Abs(Mathf.Sin(Time.time*4f));
material.color = Color.Lerp(Color.white, highlight_color, ratio);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With