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 yathi · Sep 24, 2013 at 09:19 AM · c#beginnerdaycycle

why doesn't the background color of my camera change

i'm trying to make a day and night cycle in unity. I managed to get the background color of my camera to change from black(Night) to blue(day) slowly in the morning but I try to change it back to black in the afternoon it doesn't work. What am I doing wrong?

 public class GameWorld : MonoBehaviour {
     public float igametime;
     public GameObject moon;
     public GameObject sun;
 public Camera mainCamera;
     public Vector4 SkyColour;
     public int gametimeSpeed;
     
     public void Start()
     {
         sun.light.enabled = false;
         moon.light.enabled = false;
     }
     public void Update()
     {
         igametime = Time.time * gametimeSpeed;
 
         while (igametime > 1440)
             igametime -= 1440;
         
         sun.transform.Rotate( new Vector3(0.2f, 0, 0) * Time.deltaTime * gametimeSpeed);
         moon.transform.Rotate(new Vector3(0.333333f, 0, 0) * Time.deltaTime * gametimeSpeed);
         // Dawn (this works)
         if (igametime > 0 && igametime <= 60)
         {    
             sun.light.enabled = false;
             moon.light.enabled = false;

             SkyColour = new Vector4(0.0105f, 0.0110f, 0.0176f, 0) *igametime;
             mainCamera.backgroundColor = SkyColour;
         }
         // Day
         else if (igametime > 60)
         {
             sun.light.enabled = true;                
             moon.light.enabled = false;
         }
         // Dusk (this dosen't work)
         else if (igametime > 960 && igametime <= 1020)
         {
             sun.light.enabled = false;    
             moon.light.enabled = false;
             
             mainCamera.backgroundColor = new Vector4(0.63f - 0.0105f * (igametime - 960), 0.66f - 0.0110f * (igametime - 960), 
                         1.056f - 0.0176f * (igametime - 960), 1) ;
         }
         // Night
         else if (igametime > 1020 && igametime <= 1440)
         {
             sun.light.enabled = false;    
             moon.light.enabled = true;
            }
         else 
         {
             moon.light.enabled = false;
             sun.light.enabled = false; 
         }
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 ArkaneX · Sep 24, 2013 at 11:15 AM 0
Share

As a side note - I would change this code:

 igametime = Time.time * gametimeSpeed;
  
 while (igametime > 1440)
     igametime -= 1440;

to:

 igametime += Time.time * gametimeSpeed;
  
 if (igametime > 1440)
     igametime -= 1440;

In your implementation, changing gametimeSpeed at runtime, can result in instant daytime change. For example, if Time.time is equal to 600 and speed is 1, changing speed to 2 will cause instant jump from day to night (1200).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ArkaneX · Sep 24, 2013 at 11:01 AM

I doesn't work, because when your igametime variable is greater than 60, then you always end up in the first else (Day). You need to change it to:

 else if (igametime > 60 && igametime <= 960)
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

16 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How would I make a ListChangedEvent? 1 Answer

Problem with rotation Please Help Me 1 Answer

I have a problem with coins 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