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 CloverKitsune · Apr 20, 2013 at 07:17 AM · c#colortimelerpdeltatime

Time.deltaTime making color lerp appear fast, but won't reach 1

I'm scripting in C# and having an issue with a color lerp. I set up an object to fade out with Time.deltaTime (I wanted the user to be able to set the fadeDuration in seconds, for example, 5), it fades out smoothly, but much too quickly, and when I check the fadeTimer, it's still crawling up at a tiny fraction of 1, which doesn't seem to make sense. Does anyone know how I can make the duration more accurate? I want to destroy this thing when the timer reaches > 1 which isn't happening with this code I've got.

 void Update ()
     {
         if (startFade)                                            // fade enabled
         {
             print (fadeTimer);
             if (fadeTimer < 1)
             {
                 renderer.material.color = Color.Lerp (renderer.material.color, Color.clear, fadeTimer);
                 fadeTimer += Time.deltaTime / fadeDuration;        // advance timer by fractions
             }
             else
             {
                 Destroy (gameObject);                            // once lerp is done (timer = 1), delete object
             }
         }
     } 
Comment
Add comment · Show 1
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
avatar image fafase · Apr 20, 2013 at 09:55 AM 0
Share

Works fine with me actually.

1 Reply

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

Answer by whydoidoit · Apr 20, 2013 at 11:27 AM

Your problem is that you are lerping from the already lerped colour. For the effect you are looking for you need to store the original colour when the lerp starts and continue to Lerp from that. The way you have it now:

Lets say the orginal colour is red, and we'll ignore the other components:

The first time you call Lerp the value of red assume a value between 1 and 0 - let's say it goes 1/8 the way, so now the value is 0.875 - next time around you are no longer lerping between 1 and 0, you are now lerping between 0.875 and 0 so the same 1/8 the way would have a much greater effect (we are now 1/4 the way there) than the first step and the value would become 0.66 etc etc

Comment
Add comment · Show 6 · 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
avatar image whydoidoit · Apr 20, 2013 at 11:41 AM 0
Share

If fadeDuration isn't tiny and isn't being modified I can't work out why your timer doesn't reach 1 - I'm guessing you've set fadeDuration to a long time to make the effect closer to what you wanted?

avatar image fafase · Apr 20, 2013 at 12:31 PM 0
Share

His issue seems to be with fadeTimer but not the color. The if checks if fadeTimer is under 1 so there should be a time when fadeTimer turns out to be more than one inside the if to get the if false on next update. For some reasons, my trial works but his goes wrong somewhere...

avatar image whydoidoit · Apr 20, 2013 at 12:36 PM 0
Share

$$anonymous$$y thought is that it's fading so fast that he's set the value of fadeDuration to be very long - the image is long gone, but the timer hasn't reached one yet.

avatar image fafase · Apr 20, 2013 at 12:39 PM 0
Share

Oooh I think I see now. The OP expects to reach transparency only at the end of the Lerp when it is already invisible way before. So yep @Clover$$anonymous$$itsune, your object is already almost invisible when you are about 0.1.

avatar image CloverKitsune · Apr 20, 2013 at 05:37 PM 1
Share

Gah! I've learned from that mistake before-- can't believe I overlooked that. :/

Show more comments

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

14 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

Related Questions

Smooth gradient between colours? 1 Answer

single color lerp 1 Answer

c# wait script 2 Answers

Make something change color for a set amount of time 3 Answers

What is wrong with this script ? if duration is 10 why the transform is moving in 11 seconds and not 10 ? 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