Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created December 16, 2015 18:09
Show Gist options
  • Save unitycoder/68676f7a3094ee3c029a to your computer and use it in GitHub Desktop.
Save unitycoder/68676f7a3094ee3c029a to your computer and use it in GitHub Desktop.
Lerp Material Color inside Update Loop
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LerpColor : MonoBehaviour {
public float lerpDuration = 2;
public Slider slider;
public Text textTime;
Material mat;
Color originalColor;
Color targetColor = Color.clear;
float lerpCounter = 0;
bool isLerping = false;
float timeCounter=0;
void Start ()
{
mat = GetComponent<Renderer> ().material;
originalColor = mat.color;
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.Space))
{
lerpCounter = 0;
isLerping = !isLerping;
slider.value = lerpCounter;
timeCounter = 0;
textTime.text = "Lerp: " + lerpCounter + " | Time: " + timeCounter;
mat.color = originalColor;
}
if (isLerping)
{
lerpCounter += Time.deltaTime/lerpDuration;
mat.color = Color.Lerp (originalColor, targetColor, lerpCounter);
timeCounter += Time.deltaTime;
slider.value = lerpCounter;
textTime.text = "Lerp: " + lerpCounter + " | Time: " + timeCounter;
if (lerpCounter >= 1) {
isLerping = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment