- Home /
Sprite Color Lerp during Length of Sprite Animation
Hey all,
I'm a bit stuck and for some reason I can't figure this math out.
So when I tap the screen in my game it triggers the main character sprite to play an animation (0.75 seconds long) and it triggers a method that lerps the color of the sprite using Color.Lerp(old, new, timeValue);
Now my problem is that I want the lerping to last as long as the animation so a total of .75 seconds of Lerpage! But that doesn't happen... my math causes my lerping to finish when the animation is only half way complete.
I'm using the following code can anyone help me figure out my error?
private IEnumerator ColorLerp()
{
lerpingColors = true;
bool doneFading = false;
float time = 0;
float scaledValue = 0;
//then start fading based on animation
Debug.Log ("Total Animation time = : " + totalAnimationLength);
while (!doneFading)
{
time += Time.deltaTime;
scaledValue = time / totalAnimationLength;
Debug.Log("Time : " + time + " Value: " + scaledValue + " " + mySpriteRenderer.color);
mySpriteRenderer.color = Color.Lerp(mySpriteRenderer.color, GetColor(), scaledValue);
yield return 0;
if(mySpriteRenderer.color == GetColor()) Debug.LogWarning("Color Lerping finished but time isnt");
if(scaledValue >= 1f || mySpriteRenderer.color == GetColor())
{
doneFading = true;
mySpriteRenderer.color = Color.Lerp(mySpriteRenderer.color, GetColor(), 1f);
}
}
lerpingColors = false;
if(GodMode) GodMode = false;
Debug.Log ("Done Lerping Colors " + scaledValue + " " + totalAnimationLength);
}
when scaledValue == .5~ the lerping is already at the end color return variable from the GetColor() method.
Thanks for any help.
Answer by ATMEthan · Apr 24, 2015 at 02:46 PM
Solved: it wasn't my math that was the problem. Problem was I was using the updated sprite rendere color instead of a from color value. So my from value was constantly updating messing up my lerping
Here is my updated method that works
private IEnumerator ColorLerp()
{
lerpingColors = true;
bool doneFading = false;
float time = 0;
float scaledValue = 0;
Color fromColor = mySpriteRenderer.color;
//then start fading based on animation
Debug.Log ("Total Animation time = : " + totalAnimationLength);
while (!doneFading)
{
time += Time.deltaTime;
scaledValue = time / totalAnimationLength;
Debug.Log("Time : " + time + " Value: " + scaledValue + " " + mySpriteRenderer.color);
mySpriteRenderer.color = Color.Lerp(fromColor, GetColor(), scaledValue);
yield return 0;
if(mySpriteRenderer.color == GetColor()) Debug.LogWarning("Color Lerping finished but time isnt");
if(scaledValue >= 1f || mySpriteRenderer.color == GetColor())
{
doneFading = true;
}
}
//just to be sure
mySpriteRenderer.color = GetColor();
lerpingColors = false;
if(GodMode) GodMode = false;
Debug.Log ("Done Lerping Colors " + scaledValue + " " + totalAnimationLength);
}
Your answer
Follow this Question
Related Questions
Dependency sprite animation from the variable 0 Answers
sprite changes in game 2 Answers
Lerp or animate? 1 Answer
How can I animate a sprite sheet in Java 1 Answer
Rotate a Sprite Without Using Its Pivot Or Parent GameObject 1 Answer