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 nongenre · Jun 26, 2012 at 01:06 AM · cameracolorlerpbackground

Randomly Transition Camera Background Color?

This is really just a cute detail I was going to throw on my camera quickly, but it turned into a hurdle - conceptually what I want to do is really simple, but I'm missing something. With the code below, my BG just goes to white and stays there, even though the debug log shows valid RGBA values looping through. I've tried both lerping and straight setting the camera Bg color (as commented out below)

Any ideas what I'm getting wrong here?

 var bgColor : Color; 
 
 var blooping : boolean = true;

 function Start () {
 StartCoroutine(bgColorShifter());

}

 function bgColorShifter(){    
 while (blooping){
     bgColor.r = Random.Range(0,255);
     bgColor.g = Random.Range(0,255);
     bgColor.b = Random.Range(0,255);
     bgColor.a = Random.Range(0,255);
     Debug.Log("bgColor: "+bgColor);
     var currentColor = Camera.main.backgroundColor;
     //Camera.main.backgroundColor = bgColor;
     Camera.main.backgroundColor = Color.Lerp(currentColor, bgColor, 0.1);
     yield WaitForSeconds(1.0);
 }

}

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 Bunny83 · Jun 26, 2012 at 01:55 AM 0
Share

Just like to point out that, even when your values are in the range of 0.0 to 1.0, you change the "target-color" each iteration and you just lerp 10% towards this color each time. This seems to be a little bit strange. Even if you want a real random color, i guess you want to lerp slowly over time to that color, right?

2 Replies

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

Answer by Berenger · Jun 26, 2012 at 01:47 AM

The color of the background should be updated, unless you're not actually seeing the background but an object in front of the camera.

Anyway, you're code isn't updating correctly. Every second, you pick a new random color (note that Color are between 0 and 1, your random will only create colors like (0,1,0,0) or (1,1,0,1). Then you change the background to 90% of that new color and 10% of the previous color. You should try this :

 var bgColor : Color;    
 var blooping : boolean = true;

 function Start () {
     StartCoroutine(bgColorShifter());
 }

 function bgColorShifter()
 {
     while (blooping)
     {
         bgColor.r = Random.value; // value is already between 0 and 1
         bgColor.g = Random.value;
         bgColor.b = Random.value;
         bgColor.a = 1.0; // I don't think alpha matters    
         Debug.Log("bgColor: "+bgColor);

         var t: float = 0f
         var currentColor = Camera.main.backgroundColor;
         while( t < 1.0 )
         {
             Camera.main.backgroundColor = Color.Lerp(currentColor, bgColor, t );
             yield null; // Wait one frame
             t += Time.deltaTime;
         }
     }
 }
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 Bunny83 · Jun 26, 2012 at 01:58 AM 0
Share

Yes, that's what i had in $$anonymous$$d ;)

avatar image nongenre · Jun 26, 2012 at 02:02 AM 0
Share

Awesome - this worked great. $$anonymous$$inor thing, though - "yield null;" throws me a strange compiler error in $$anonymous$$onodevelop. I used waitForseconds() anyway, since that let me control how gradual the colors shift, but I wanted to mention it since I didn't understand the compiler error and haven't seen that use of yield before.

avatar image Berenger · Jun 26, 2012 at 03:19 AM 0
Share

I use javascript only here, but I don't think there should be an error. Anyway, if it works for you ^^

avatar image
2

Answer by hathol · Jun 26, 2012 at 01:45 AM

From the script reference: "Each color component is a floating point value with a range from 0 to 1." So just use Random.value instead of Random.Range() and you should be fine :)

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 nongenre · Jun 26, 2012 at 01:59 AM 0
Share

Derp! Good point.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

How do I loop a Color32.Lerp? 3 Answers

HSV to RGB 1 Answer

Change the background color attribute of a camera in C#? 2 Answers

How can I create a gradient color background for my game, without using skybox? 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