Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges