Using Lerp to create a hovering effect for a GameObject
Posted
by
OhMrBigshot
on Game Development
See other posts from Game Development
or by OhMrBigshot
Published on 2012-07-05T10:57:52Z
Indexed on
2012/07/05
15:24 UTC
Read the original article
Hit count: 334
I want to have a GameObject that has a "hovering" effect when the mouse is over it.
What I'm having trouble with is actually having a color that gradually goes from one color to the next.
I'm assuming Color.Lerp()
is the best function for that, but I can't seem to get it working properly.
Here's my CubeBehavior.cs
's Update()
function:
private bool ReachedTop = false;
private float t = 0f;
private float final_t;
private bool MouseOver = false;
// Update is called once per frame
void Update () {
if (MouseOver) {
t = Time.time % 1f; // using Time.time to get a value between 0 and 1
if (t >= 1f || t <= 0f) // If it reaches either 0 or 1...
ReachedTop = ReachedTop ? false : true;
if (ReachedTop) final_t = 1f - t; // Make it count backwards
else final_t = t;
print (final_t); // for debugging purposes
renderer.material.color = Color.Lerp(Color.red, Color.green, final_t);
}
}
void OnMouseEnter() {
MouseOver = true;
}
void OnMouseExit() {
renderer.material.color = Color.white;
MouseOver = false;
}
Now, I've tried several approaches to making it reach 1 then count backwards till 0 including a multiplier that alternates between 1 and -1, but I just can't seem to get that effect. The value goes to 1 then resets at 0.
Any ideas on how to do this?
© Game Development or respective owner