- Home /
Question by
aweshum · Mar 05, 2018 at 05:25 AM ·
c#lerponmouseoverfadeinonmouseexit
How do I lerp to a color?
I have a sprite that, when I mouse over it, I want it to appear slowly. Like a glow behind the player. But I don't know how to do it exactly.
I tried using lerp. And I got nothing. The sprite appears on OnMouseEnter and dissapears in OnMouseExit.
I tried OnMouseOver as well.
So far nothing seems to work.
Comment
Answer by yummy81 · Mar 05, 2018 at 06:43 AM
Put your sprite in the scene, add collider to it and the script below. You can use OnMouseEnter simply turning it into coroutine (or you can create a separate coroutine and run it from "normal" OnMouseEnter). Here's the code:
private SpriteRenderer sr;
private void Awake()
{
sr = GetComponent<SpriteRenderer>();
sr.color = new Color(1f, 1f, 1f, 0f);
}
IEnumerator OnMouseEnter()
{
Color start = sr.color;
Color target = new Color(1f, 1f, 1f, 1f);
float t = 0f;
float duration = 3f;
while(t < 1f)
{
t += Time.deltaTime / duration;
sr.color = Color.Lerp(start, target, t);
yield return null;
}
}
Your answer