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 Addyarb · Dec 28, 2014 at 01:28 AM · color.lerp

Color Lerp Problems

Hey there Unity, having some strange troubles with my color lerping.

The SlerpToBlack() function runs as expected, but as soon as it gets done and hits color.black, everything pops back to white, and it all stops.

Maybe I'm going about this the wrong way? I've commented out other color sources, but they are affected the same way:

 public class DayAndNight : MonoBehaviour {
     public Color directionalLightStarting;
     //public Color fogStarting;
     //private Color ambientLightColor;
     private Color directionalLightColor;
     //private Color fogColor;
     //public Color ambientLightStarting;
     public Light mainDirectionalLight;
     public bool slerpToBlack = true;
     public bool slerpToWhite = false;
     public float speed = 0.05f;
     // Use this for initialization
     void Start () {
         mainDirectionalLight = GetComponent<Light> ();
     }
     
     void Update(){
         if (slerpToBlack) {
                     SlerpToBlack ();
                 }
         if (slerpToWhite) {
                     SlerpToWhite();
                 }
     }
     public void SlerpToBlack(){
         //ambientLightColor = Color.Lerp (ambientLightStarting, Color.black, Time.time * speed);
         //fogColor = Color.Lerp (fogStarting, Color.black, Time.time * speed);
         directionalLightColor = Color.Lerp (directionalLightStarting, Color.black, Time.time * speed);
         mainDirectionalLight.color = directionalLightColor;
         //RenderSettings.ambientLight = ambientLightColor;
         //RenderSettings.fogColor = fogColor;
         if (mainDirectionalLight.color == Color.black) {
             slerpToBlack = false;
             slerpToWhite = true;
                 }
     }
     public void SlerpToWhite(){
         //ambientLightColor = Color.Lerp (Color.black, ambientLightStarting, Time.time * speed);
         //fogColor = Color.Lerp (Color.black, fogStarting, Time.time * speed);
         directionalLightColor = Color.Lerp (Color.black, directionalLightStarting, Time.time * speed);
         mainDirectionalLight.color = directionalLightColor;
         //RenderSettings.ambientLight = ambientLightColor;
         //RenderSettings.fogColor = fogColor;
         if (mainDirectionalLight.color == directionalLightStarting) {
             slerpToWhite = false;
             slerpToBlack = true;
         }
         }
 }
 
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
1
Best Answer

Answer by NoseKills · Dec 28, 2014 at 01:55 AM

Because of what Lerp does, as soon as Time.time * speed equals or exceeds 1, you start doing both SlerpToBlack() and SlerpToWhite() every time in Update.

After the third parameter in Color.Lerp becomes 1, it always returns the target color. That causes you to go inside the if (mainDirectionalLight.color == Color.black) in SlerpToBlack() and because you have 2 if's instead of an if-else in your Update() you go straight into SlerpToWhite() in the same Update cycle, and again (still) the third parameter is 1 so you go inside the if() in both methods all the time.

This for example should fix it

 public class DayAndNight : MonoBehaviour {
 public Color directionalLightStarting;
 //public Color fogStarting;
 //private Color ambientLightColor;
 private Color directionalLightColor;
 //private Color fogColor;
 //public Color ambientLightStarting;
 public Light mainDirectionalLight;
 public bool nightIsComing = true;
 public bool slerpToWhite = false;
 public float speed = 0.3f;
 public float lightTimer;

 public float nightDuration = 2f;
 public float nightTimer = 0f;
 // Use this for initialization
 void Start () 
 {
     mainDirectionalLight = GetComponent<Light>();
 }
 
 void Update()
 {
     if (nightTimer > 0)
     {
         nightTimer -= Time.deltaTime;
     }
     else
     {
         lightTimer += Time.deltaTime * speed;
         if (nightIsComing)
         {
             SlerpToBlack ();
         }
         else
         {
             SlerpToWhite();
         }
     }
 }

 public void SlerpToBlack(){
     //ambientLightColor = Color.Lerp (ambientLightStarting, Color.black, Time.time * speed);
     //fogColor = Color.Lerp (fogStarting, Color.black, Time.time * speed);
     directionalLightColor = Color.Lerp (directionalLightStarting, Color.black, lightTimer * speed);
     mainDirectionalLight.color = directionalLightColor;
     //RenderSettings.ambientLight = ambientLightColor;
     //RenderSettings.fogColor = fogColor;
     if (mainDirectionalLight.color == Color.black) {
         nightIsComing = !nightIsComing;
         nightTimer = nightDuration;
         lightTimer = 0;
     }
 }
 public void SlerpToWhite(){
     //ambientLightColor = Color.Lerp (Color.black, ambientLightStarting, Time.time * speed);
     //fogColor = Color.Lerp (Color.black, fogStarting, Time.time * speed);
     directionalLightColor = Color.Lerp (Color.black, directionalLightStarting, lightTimer * speed);
     mainDirectionalLight.color = directionalLightColor;
     //RenderSettings.ambientLight = ambientLightColor;
     //RenderSettings.fogColor = fogColor;
     if (mainDirectionalLight.color == directionalLightStarting) {
         nightIsComing = !nightIsComing;
         lightTimer = 0;
     }
 }

}

Comment
Add comment · Show 4 · 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 Addyarb · Dec 28, 2014 at 03:02 AM 0
Share

Works perfectly! Thanks so much for your help. Never knew color.lerp worked like this.

Could you perhaps explain how to keep it night for a defined amount of time?

avatar image NoseKills · Dec 28, 2014 at 02:43 PM 0
Share

Please click the checkmark next to the answer to mark this question as answered if you think the original problem is solved.

I edited the answer to have a variable for night duration.

If you want to make the effect even more natural looking, you should probably use $$anonymous$$athf.Sin() to do the Lerping, but that's a topic for another Question...

avatar image Addyarb · Dec 29, 2014 at 12:23 AM 0
Share

I'm very interested in learning about how I could make it more realistic! Thank you for your responses, they've been very helpful.

avatar image NoseKills · Dec 29, 2014 at 02:20 AM 0
Share

AGAIN : Please click the check mark next to the answer to mark this question as answered.

That's how Unity Answers works. Otherwise this question will show as "unanswered" and people will waste time trying to solve a solved question

You're welcome

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Color LERP problem 1 Answer

Color.lerp help (GUI.color) 1 Answer

Spawned Object Gradient Mesh Colors? 2 Answers

Can you change Alpha and Blue(rbg) simultaneously? 3 Answers

smoothly switch between 2 colors that are using lerp 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