• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ATMEthan · Apr 24, 2015 at 02:33 PM · animationspritelerpcolor.lerpsprite animation

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
     }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dependency sprite animation from the variable 0 Answers

Small video files or sprite sheet animations? 0 Answers

Color.lerp help (GUI.color) 1 Answer

Sprite Stop Animation on different images 0 Answers

In a 2D animation, can I set a diffrent speed per sprite? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges