• 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 ironblock · Aug 15, 2014 at 12:34 PM · c#colorlerp

Lerping 10 collors not that smooth

the idea is that each level has it own color and that it lerps from one level to the other one.

old : my script witch lerps between 2 colors every level (for 10 levels long) but it does not change very smooth. it has to change consistently thrue out the level bet each time it changes its a quite big color difference.

new : when it lerps the color change is seamless but it does not always lerp because it does not always the exact color because of the lerping

edit: new code.

this is the script (you do need to change the colors manually so you can see the difference) using UnityEngine; using System.Collections;

 public class ColorChanger : MonoBehaviour {
     public Color ambColor1 = Color.red;
     public Color ambColor2 = Color.blue;
     public Color fogColor1 = Color.red;
     public Color fogColor2 = Color.blue;
     public float duration = 1.0F;
     public Color fogColor3 = Color.blue;
     public Color fogColor4 = Color.blue;
     public Color fogColor5 = Color.blue;
     public Color fogColor6 = Color.blue;
     public Color fogColor7 = Color.blue;
     public Color fogColor8 = Color.blue;
     public Color fogColor9 = Color.blue;
     public Color fogColor10 = Color.blue;
     public Color fogColor11 = Color.blue;
 
 
     public Color ambColor3 = Color.red;
     public Color ambColor4 = Color.blue;
     public Color ambColor5 = Color.red;
     public Color ambColor6 = Color.blue;
     public Color ambColor7 = Color.red;
     public Color ambColor8 = Color.blue;
     public Color ambColor9 = Color.red;
     public Color ambColor10 = Color.blue;
     public Color ambColor11 = Color.red;
 
 
     public int map;
 
     public bool LerpColorBool = true;
 
     public GameObject DirectionalLight;
     public GameObject levelManager;
     // Use this for initialization
     void Start () {
         StartCoroutine (colorCheck ());
     }
     
     // Update is called once per frame
     void Update () {
         Debug.Log (ambColor1);
         if (LerpColorBool == true)
         {
             LerpColor();
         }
     
 
 
     }
 
     void LerpColor()
     {
                 float t = Mathf.PingPong (Time.time, duration) / duration;
                 RenderSettings.fogColor = Color.Lerp (fogColor1, fogColor2, t);
                 RenderSettings.ambientLight = Color.Lerp (ambColor1, ambColor2, t);
                 DirectionalLight.light.color = Color.Lerp (ambColor1, ambColor2, t);
                 if (RenderSettings.fogColor == fogColor2) {
                         LerpColorBool = false;
     
                 }
         }
     IEnumerator colorCheck ()
     {
         yield return new WaitForSeconds(1f);
 
         //LevelManager LM = levelManager.GetComponent<LevelManager> ();
         if (LerpColorBool == false) {
             map ++;
                         switch (map) {
                         case 1:
                                 
                                 break;
                         case 2:
                                 fogColor2 = fogColor3;
                                 fogColor1 = fogColor2;
                                 ambColor2 = ambColor3;
                                 ambColor1 = ambColor1;
                                 break;
                         case 3:
                                 fogColor2 = fogColor3;
                                 fogColor1 = fogColor4;
                                 ambColor2 = ambColor3;
                                 ambColor1 = ambColor4;
                                 break;
                         case 4:
                                 fogColor2 = fogColor4;
                                 fogColor1 = fogColor5;
                                 ambColor2 = ambColor4;
                                 ambColor1 = ambColor5;
                                 break;
                         case 5:
                                 fogColor2 = fogColor5;
                                 fogColor1 = fogColor6;
                                 ambColor2 = ambColor5;
                                 ambColor1 = ambColor6;
                                 break;
                         case 6:
                                 fogColor2 = fogColor6;
                                 fogColor1 = fogColor7;
                                 ambColor2 = ambColor6;
                                 ambColor1 = ambColor7;
                                 break;
                         case 7:
                                 fogColor2 = fogColor7;
                                 fogColor1 = fogColor8;
                                 ambColor2 = ambColor7;
                                 ambColor1 = ambColor8;
                                 break;
                         case 8:
                                 fogColor2 = fogColor9;
                                 fogColor1 = fogColor8;
                                 ambColor2 = ambColor9;
                                 ambColor1 = ambColor8;
                                 break;
                         case 9:
                                 fogColor2 = fogColor9;
                                 fogColor1 = fogColor10;
                                 ambColor2 = ambColor9;
                                 ambColor1 = ambColor10;
                                 break;
                         case 10:
                                 fogColor2 = fogColor11;
                                 fogColor1 = fogColor10;
                                 ambColor2 = ambColor11;
                                 ambColor1 = ambColor10;
                                 break;
                     
             
                         }
             LerpColorBool = true;
         }
         StartCoroutine (colorCheck ());
     }
 
 }
 
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

Answer by smoggach · Aug 15, 2014 at 02:01 PM

Well your question makes it sound like you're just using the wrong colors but from your code I can guess that changing your lerp targets every second is probably the cause of your problem.

Comment
Add comment · Show 3 · 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 ironblock · Aug 15, 2014 at 02:21 PM 0
Share

i thought this would help:

 if (RenderSettings.fogColor == fogColor2) {
                         LerpColorBool = false;
     
                 }

but since it lerps it never actually hits the exact color. but i do think a single lerp than changing the color and lerping again would fix it.

avatar image smoggach · Aug 15, 2014 at 02:27 PM 0
Share

Yar, with lerping u generally have to set it to the exact value u want when you're finished.

avatar image ironblock · Aug 15, 2014 at 02:43 PM 0
Share

but int only triggers the if 2% of the time. so that is not exactly true in this case . with some duration times it hits it more often but i have not found one that does it every time.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Multiple Cars not working 1 Answer

Time.deltaTime making color lerp appear fast, but won't reach 1 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

Vertex coloring isn't working 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