Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Lynx2099 · Sep 01, 2016 at 05:11 AM · c#coroutinelerp

Lerping alpha with coroutine only lerping first time it is triggered by event

I've checked the existing questions as well as implementing an example from the Unity manual, but for some reason I am getting an identical issue no matter which solution I implement. An icon's alpha lerps gradually from 1 to 0 the first time the coroutine is called, but subsequent calls go directly from one extreme to the other without lerping. (More info below code)

Here is the currently implemented code for the coroutine:

 [SerializeField]
 Image lightHUD;

 [SerializeField]
 Image darkHUD;

 void Start () 
 {
     EventManager.Instance.OnVisible += OnLighten;
     EventManager.Instance.OnInvisible += OnDarken;
 }

 void OnDestroy()
 {
     EventManager.Instance.OnVisible -= OnLighten;
     EventManager.Instance.OnInvisible -= OnDarken;
 }
 void OnLighten()
 {
     StartCoroutine(FadeToLight());
 }

 void OnDarken()
 {
     StartCoroutine(FadeToDark());
 }

 IEnumerator FadeToDark()
 {
     Color cl = lightHUD.color;
     Color cd = darkHUD.color;

     for (float f = 1f; f >= 0; f -= 1/100f)
     {            
         cl.a = f;
         lightHUD.color = cl;

         cd.a = 1 - f;
         darkHUD.color = cd;

         yield return null;
     }
 }

 IEnumerator FadeToLight()
 {
     Color cl = lightHUD.color;
     Color cd = darkHUD.color;

     for (float f = 1f; f >= 0; f -= 1/100f)
     {
         cl.a = 1 - f;
         lightHUD.color = cl;

         cd.a = f;
         darkHUD.color = cd;

         yield return null;
     }
 }

I currently have an event system coded in one of my scripts' Update() functions. This calls the functions in the HUD script above. Basically, if the character is near a light and visible, a brightly colored HUD icon's alpha moves up to indicate that he is visible. When the player is in the dark, this icon's alpha moves down and a similar but darker icon's alpha moves up to indicate he is in the dark.

So far, so good. This part works properly, but only once. Anytime afterward, when walking toward or away from a light, the alpha for both icons jumps straight to their respective extremes after the allotted lerp time, but without lerping (e.g. lerp is time is 1 sec, character walks into light, nothing changes for 1 sec, then bright icon's alpha jumps right to 1 from 0 and dark jumps right to 0 from 1)

Any ideas? Thanks in advance!

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 ionside · Sep 01, 2016 at 05:16 AM 0
Share

You might want to try putting those for loops inside a while(true) loop. Perhaps?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lynx2099 · Sep 02, 2016 at 10:45 PM

Issue resolved. As described, the event was being called in an update, causing multiple coroutines to be initiated. These conflicted coroutines had the effect described above as the result was jumping from one coroutine's end product to another, only matching when the majority of them were at the extremes (or something along those lines.

Ideally, an event should only be triggered once, but in my case I resolved this issue by eliminating the coroutine and using lerp on a light version of the image, which in effect was called as if in update, and a dark version underneath. This gave me a nice fade transition. Here is the final, functional code:

 [SerializeField]
 Image lightHUD;

 void Start () 
 {
     EventManager.Instance.OnVisible += LightenIcon;
     EventManager.Instance.OnInvisible += DarkenIcon;
 }

 void OnDestroy()
 {
     EventManager.Instance.OnVisible -= LightenIcon;
     EventManager.Instance.OnInvisible -= DarkenIcon;
 }

 void LightenIcon()
 {
     Color newLightColor = Color.Lerp(lightHUD.color, new Color(lightHUD.color.r, lightHUD.color.g, lightHUD.color.b, 1), 0.1f);
     lightHUD.color = newLightColor;
 }

 void DarkenIcon()
 {
     Color newLightColor = Color.Lerp(lightHUD.color, new Color(lightHUD.color.r, lightHUD.color.g, lightHUD.color.b, 0), 0.1f);
     lightHUD.color = newLightColor;
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Help using Lerp inside of a coroutine. 1 Answer

better way to rotate instead Coroutine 1 Answer

Multiple Cars not working 1 Answer

Why is my Mathf.Lerp not lasting for the intended duration, even though I'm not using Time.time as the t variable? 1 Answer

Distribute terrain in zones 3 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