- Home /
Lerping/Fading between multiple colours
Hi
I have been searching and going through the docs for how to successfully fade from one colour to another but after a few hours of trial and error, have come up still unsure how to achieve this. I thought lerping between colours would be simple enough =(
I have some colliders in the scene and when the player object hits them, it should change the colour of a skysphere I have. At the moment, it changes straight away but I'm after a fade/transition effect so its not so jarring.
I tried the fade script from Unify Wiki: http://wiki.unity3d.com/index.php?title=Fade but could not get this working either. I have been looking at coroutines but am a bit lost with them.
Anyone have some ideas as to how to get this going? The material colour is passed in from another script.
Thanks
using UnityEngine;
using System.Collections;
public class ChangeSkysphereColour : MonoBehaviour {
public static string materialColour;
public Color blue = new Color (129.0F,155.0f, 255.0f);
public Color green = new Color (160.0F,255.0f, 170.0f);
public Color pink = new Color (253.0F,100.0f, 253.0f);
public Color yellow = new Color (255.0F,148.0f, 179.0f);
public Color white = new Color (228.0F,228.0f, 228.0f);
private Color currentColour;
// Use this for initialization
void Start () {
currentColour = yellow;
}
// Update is called once per frame
void Update () {
//renderer.material.color = currentColour;
if(materialColour == "Blue")
{
renderer.material.color = Color.Lerp(currentColour,blue, 0.1f);
currentColour = blue;
}
if(materialColour == "Green")
{
renderer.material.color = Color.Lerp(currentColour,green, 0.1f);
currentColour = green;
}
if(materialColour == "Yellow")
{
renderer.material.color = Color.Lerp(currentColour,yellow, 0.1f);
currentColour = yellow;
}
if(materialColour == "Pink")
{
renderer.material.color = Color.Lerp(currentColour,pink, 0.1f);
currentColour = pink;
}
if(materialColour == "White")
{
renderer.material.color = Color.Lerp(currentColour,white, 0.1f);
currentColour= white;
}
}
}
Answer by whydoidoit · Apr 01, 2013 at 12:16 PM
Lerp's last parameter is a number between 0 and 1 representing the current level between the two.
string lastMaterialColour;
float t;
// Update is called once per frame
void Update () {
if(lastMaterialColour != materialColour)
{
t= 0;
lastMaterialColour = materialColour;
}
t += Time.deltaTime;
//renderer.material.color = currentColour;
if(materialColour == "Blue")
{
renderer.material.color = Color.Lerp(currentColour,blue, t);
currentColour = blue;
}
Thanks for that. I should have realised that the value in the Lerp was just sitting there without any change in value.
However, after putting the new code in, it still just jumps between colours rather than fade between them. $$anonymous$$aybe the colour values need to be modified individually? Or would this work better on materials rather than direct colours?
Your answer
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
Material doesn't have a color property '_Color' 4 Answers
How to fade an image 1 Answer
Color lerp once? 2 Answers