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
1
Question by BilboStabbins · Apr 16, 2014 at 06:26 PM · fadefadeout

Basic screen fader fading too quickly

I have a basic fading coroutine that is used to fade a black GUITexture to Color.clear using Lerp.Color. The coroutine has a FadeSpeed variable to control the speed of the fade. However, for some reason the fade is occurring much more quickly than the FadeSpeed I have entered. That is, Color.clear (0, 0, 0, 0) is reached before t has reached 1.

Any help would be great.

     private float t = 0;

     void Awake()
     {
         // Note, Pixel inset used for pixel adjustments for size and position.
         guiTexture.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
     }
 
     void Start()
     {
         StartCoroutine(FadeToClear());
     }
 
     IEnumerator FadeToClear()
     {
         while (t < 1)
         {
             t += Time.deltaTime * (1.0f / FadeSpeed);
 
             guiTexture.color = Color.Lerp(guiTexture.color, Color.clear, t);
 
             yield return new WaitForEndOfFrame();
         } 
     }
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
3
Best Answer

Answer by robertbu · Apr 16, 2014 at 06:40 PM

The problem is that you are updating the start position of your Lerp() each frame. You want to hold the start position constant when you use Lerp() this way (with a 't' value that goes from 0.0 to 1.0). Try this:

 IEnumerator FadeToClear()
 {
     Color startColor = guiTexture.color;
     while (t < 1)
     {
         t += Time.deltaTime * (1.0f / FadeSpeed);
 
         guiTexture.color = Color.Lerp(startColor, Color.clear, t);
 
         yield return null;
     } 
 }

Also you can use 'yield return null' to execute once each frame;

Comment
Add comment · Show 2 · 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 BilboStabbins · Apr 17, 2014 at 07:39 PM 0
Share

Thanks, that did the trick!

If this is the case then I think there may be a similar problem with the S$$anonymous$$lth Game tutorial - ScreenFader script chapter (link text). This doesn't use a t value going from 0-1 but does the Lerp inside Update ins$$anonymous$$d. Changing the fadeSpeed here doesn't change the speed of the fade either.

avatar image robertbu · Apr 18, 2014 at 05:25 AM 0
Share

Note that if 'FadeSpeed' is public, only changes in the Inspector matter. Changing the initialization in code will not change the value once the script has been attached to a game object.

As for 'S$$anonymous$$lth Game Tutorial', their use of Lerp() is very different. The last parameter does not vary from 0.0 to 1.0. Ins$$anonymous$$d the last value remains at approximately the same small fraction frame to frame. The result is that the Lerp() moves towards it destination the same fraction each frame, but since the starting value is also moved, the same fraction represents less 'distance'. The result is an eased movement using Lerp(). This use of Lerp() is non-traditional, but is very common in Unity scripts. It is a very simple way to, without a timer or an easing function, get an eased movement towards some goal.

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

Fading out doesn't fade out, but instantly goes from black to clear 0 Answers

How to change Material Render Mode from Opaque to Fade during runtime? 1 Answer

Changing a GUITexture's alpha gives me no effect in game. 0 Answers

Fade logo before main menu 2 Answers

Sprite smooth fade out/in 2 Answers


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