Hi friends, thanks for taking the time to look at my question.
I have a “tile” that when clicked fluctuates between an “on” state and “off” state. As a visual cue, I would like the tiles to have an “on color” and an “off color”.
My understanding is that I would need to use Color.Lerp - lerping between the sprite’s current color and the desired color.
I’ve done so in my code here,
public class testclick : MonoBehaviour
{
bool isOn = true;
public Color on_color;
public Color off_color;
private SpriteRenderer sr;
private float timeLeft = 0f;
void Awake()
{
sr = gameObject.GetComponent<SpriteRenderer>();
}
void Update()
{
Debug.Log("Updating...");
if (timeLeft > 0)
{
Debug.Log($"IsOn: {isOn}");
if (isOn)
{
Color c = Color.Lerp(on_color, sr.color, timeLeft);
sr.color = new Color(c.r, c.g, c.b, 3);
Debug.Log($"{c.r}, {c.g}, {c.b}");
}
else
{
Color c = Color.Lerp(off_color, sr.color, timeLeft);
sr.color = new Color(c.r, c.g, c.b, 3);
Debug.Log($"{c.r}, {c.g}, {c.b}");
}
timeLeft -= Time.deltaTime;
}
}
void OnMouseDown()
{
timeLeft = 0.2f;
isOn = !isOn;
}
}
but what results is NOT a smooth transition. What do I need to do to make this more fluid, and less choppy?
Thanks again!