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 MrGarion · Nov 06, 2013 at 02:42 PM · cycledaycyclenightday

day/night cycle runs only twice

as my title says my day/night cycle only runs twice. i cant really understand why, because i have a clock that says when iGameTime is over 225 then reset to 0.

here is the script:

 using UnityEngine;
 using System.Collections;
 
 public class DayNight : MonoBehaviour {
     
     public float iGameTime;
     public float rotatingSpeed = 8f;
     public Camera mainCamera;
     public int gameTimeSpeed = 1;
     public Vector4 Skycolor;
     
     public bool dawn;
     public bool day;
     public bool dusk;
     public bool night;
     
 
     // Use this for initialization
     void Start () {
     
         dawn = false;
         day = false;
         dusk = false;
         night = false;
         
     }
     
     // Update is called once per frame
     void Update () {
         
         iGameTime = Time.time * gameTimeSpeed;
         if (iGameTime > 225)
             iGameTime -= 225;
         
     
         transform.Rotate (new Vector3 (-0.2f, 0, 0) * (Time.deltaTime * rotatingSpeed));
         
         mainCamera.backgroundColor = Skycolor;
         
         // Dawn
         if (iGameTime > 0 && iGameTime <= 56){
             
             Skycolor = new Vector4 (0.23f + (0.009f * iGameTime), 0.26f + (0.005f * iGameTime), 0.55f, 0f);
             
             
             
             dawn = true;
             day = false;
             dusk = false;
             night = false;
             
         }
         
         // Day
         else if (iGameTime > 56 && iGameTime <= 112){
             
             if (iGameTime > 56 && iGameTime <= 87){
                 
                 Skycolor = new Vector4 (0.7339369f - 0.0005f * (iGameTime - 56), 0.539965f - 0.005f  * (iGameTime - 56), 0.55f, 0);
                 
             } else if (iGameTime > 87 && iGameTime <= 112){
                 
                 Skycolor = new Vector4 (0.718458f - 0.005f * (iGameTime - 87), 0.3851757f - 0.009f * (iGameTime - 87), 0.55f, 0);
                 
             }
             
             
              
             dawn = false;
             day = true;
             dusk = false;
             night = false; 
             
         }
         
         // Dusk
         else if (iGameTime > 112 && iGameTime <= 169){
             
             Skycolor = new Vector4 (0.5937353f - 0.006f * (iGameTime - 112), 0.1606749f - 0.012f * (iGameTime - 112), 0.55f, 0);
             
             
             
             dawn = false;
             day = false;
             dusk = true;
             night = false;
             
         }
         
         // Night
         else if (iGameTime > 169 && iGameTime <= 225){
             
             Skycolor = new Vector4 (0.2521621f - 0.000496f * (iGameTime - 169), -0.5224715f + 0.0142f * (iGameTime - 169), 0.55f, 0);
             
             
             
             dawn = false;
             day = false;
             dusk = false;
             night = 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
0
Best Answer

Answer by fafase · Nov 06, 2013 at 03:14 PM

First off you should use enum instead of 4 boolean.

 enum State{Dawn,Day, Dusk, Night}

 State state= State.Dawn;

Then the reason you only get two cycles is because you use Time.time. First round your variable gets to 225 then you subtract 225 to it but Time.time continue increasing.

You'd rather use:

 iGameTime = Time.deltaTime* gameTimeSpeed;
 if (iGameTime > 225)
    iGameTime = 0;

Concerning the color of your skybox you might as well use Color.Lerp. That will change it gradually instead of the abrupt change you probably get now.

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 MrGarion · Nov 06, 2013 at 03:29 PM 0
Share

so enums works like booleans?

Thanks for the explicatory answer!

Actually, the change of colors runs pretty smoothly after a long while of testing ^^ but i will definetly check Color.Lerp out! Again thanks!

avatar image fafase · Nov 06, 2013 at 06:42 PM 0
Share

Enum are not boolean and do not work the same. They just allow you to define a list of state into one. Like I did. Then ins$$anonymous$$d of constantly having four lines to set all boolean to true or false, you only have one line:

 state = State.chosenState;

then you can check using:

 if(state == State.Night){}

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

Day Night Cycle sun fade 1 Answer

I can't get my script to work I get an error. [Please Help!] 0 Answers

How to make a Day/Night Cycle? 2 Answers

Photon Day Night Cycle Unsynced 1 Answer

Blend two lightmaps for day/night cycle? (terrain) 0 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