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 Frenzikk · Nov 02, 2014 at 04:50 PM · cameracolorlerp

How do I loop a Color32.Lerp?

I am trying to loop a smooth change in a random color. Basically, I want the camera's background color to smoothly change between a random color. Right now, the code below lerps the first loop, but then just jumps to the colors instead of being smooth. So, for some reason, the first random color change works, but all the ones after jump to the next without lerping. How could I fix this? Thank you ;) BTW, if you go to k a h o o t . i t, you can see in the background what I'm trying to achieve. :)

 var currentColor : Color32 = Color.white;
 var randomColor : Color32 = Color.white;
 var lerped : Color32 = Color.white;
 
 function Start() {
     StartCoroutine(ChangeColor()); 
 }
 
 function Update() {
     lerped = Color32.Lerp(currentColor, randomColor, Time.time / 1);
     this.camera.backgroundColor = lerped;
 }
 
 function ChangeColor() {
     while(true){
         currentColor = this.camera.backgroundColor;
         randomColor = Color32(Random.Range(0, 255), Random.Range(0, 255), Random.Range(0, 255), 1);
         yield WaitForSeconds(1);
     }
 }
 

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

3 Replies

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

Answer by robertbu · Nov 02, 2014 at 05:15 PM

The final value of Lerp() goes between 0.0 and 1.0, so you need to introduce a timer that you reset when the color changes or some other mechanism that puts the values in the correct range:

  #pragma strict
 
  var currentColor : Color32 = Color.white;
  var randomColor : Color32 = Color.white;
  var lerped : Color32 = Color.white;
  var time = 1.0;
  
  private var timer = 0.0;
  
  
  function Start() {
      StartCoroutine(ChangeColor()); 
  }
  
  function Update() {
      timer += Time.deltaTime / time;
      lerped = Color32.Lerp(currentColor, randomColor, timer);
      camera.backgroundColor = lerped;
  }
  
  function ChangeColor() {
      while(true){
          currentColor = randomColor;
          randomColor = Color32(Random.Range(0, 255), Random.Range(0, 255), Random.Range(0, 255), 1);
          timer = 0.0;
          yield WaitForSeconds(time);
      }
  }
Comment
Add comment · Show 1 · 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 Frenzikk · Nov 02, 2014 at 05:19 PM 0
Share

YES! THNA$$anonymous$$ YOU!! ;)

avatar image
1

Answer by sysameca · Nov 02, 2014 at 05:52 PM

Guess i am too late, but here is how ill code it:

 public class ColorChange : MonoBehaviour {
 
     public float timeInterval = 1.0f;
     public float speed = 2.0f;
 
     void Start () {
         StartCoroutine(ColorChangeRoutine());
     }
 
     private IEnumerator ColorChangeRoutine()
     {
         float timer = 0.0f;
         Color32 color = new Color32((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 255);
       
         while (timer < timeInterval)
         {
             timer += Time.deltaTime * speed;
             camera.backgroundColor = Color32.Lerp(camera.backgroundColor, color, timer);
         
             yield return null;
         }
 
         // On finish recursively call the same routine
         StartCoroutine(ColorChangeRoutine());
     }
 
 }


I was in the middle of the code when i saw there was already one :)

Comment
Add comment · Show 1 · 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 robertbu · Nov 02, 2014 at 05:54 PM 0
Share

Thumbs up since this is cleaner. Could also do it the other way around and roll the random code into Update() and skip the coroutine. I decided to stay close to his original code.

avatar image
-1

Answer by Nebukam · Nov 02, 2014 at 04:59 PM

You're using 'Time.time/1', which is actually a constantly increasing value. As per unity doc : "This is the time in seconds since the start of the game", so instead you need deltaTime "The time in seconds it took to complete the last frame"

  lerped = Color32.Lerp(currentColor, randomColor, Time.deltaTime);



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 Frenzikk · Nov 02, 2014 at 05:02 PM 0
Share

Now it doesn't work at all. :( Any help??

avatar image Nebukam · Nov 02, 2014 at 05:54 PM 0
Share

Sorry forgot one line

lerped = Color32.Lerp(currentColor, randomColor, Time.deltaTime); currentColor = lerped;

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Randomly Transition Camera Background Color? 2 Answers

How to randomly chenge camerabackground color every 5 seconds? 1 Answer

Lerping Field Of View is buggy 1 Answer

Smooth gradient between colours? 1 Answer

(Vector3.Lerp) Camera doesn't reach the specified Destination! 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