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 milincjoshi · Mar 31, 2015 at 10:47 AM · directional lightnightday

How do i achieve day and night change in real time ?

Hi, I've used following code for achieving day and night changes in real time using a directional light. I need to set the initial position of the sun (directional light ) according to system's time. Say user starts the game at 12pm then the sun should start from 0 degrees and rotate 90 degrees in anti clockwise direction, I've used following code but the initial position is not being set in it. Kindly help me with this code

 void Update()
     {
         DateTime dt  = DateTime.Now;
         float hourSec = (dt.Hour * 60 * 60);
         float minuteSec = (dt.Minute * 60);
         float sec = dt.Second;
         float currentSec = hourSec + minuteSec + sec;
         int divideFactor = 24 * 60 * 60;
         float divident = currentSec * 360;
         float degrees = divident / divideFactor;
 
         //set flag 0
         if (setInitialRotation)//wait and run initial rotation 
         {
             transform.LookAt (target.transform);
             transform.RotateAround (target.transform.position, Vector3.forward, degrees * Time.deltaTime  );
             setInitialRotation = false;
         }
         else//run final rotation, set flag 1
         {
             transform.LookAt (target.transform);
             transform.RotateAround (target.transform.position, Vector3.forward, (float)0.004166 * Time.deltaTime);
         }
 
     }
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 KdRWaylander · Apr 01, 2015 at 07:45 AM

Hi,

I'm not very convinced by your math about seconds (i just gave it a quick look maybe you can explain it a bit more): if it's midnight in real world you'll have 24*60*60 seconds thus degrees will be equal to 360 or 0 (same thing) thus your directional light will be horizontal, which is ok for a night mood. But if it's noon, you'll have 180 degrees which will give you the same lighting (in the opposite direction) where you would want the sun to be facing the ground (which is 90 degrees).

What you could do is a switch case with your "dt.Hour" and select the good angle (if you want to be more precise you can do a sub switch case with dt.Minute), angle that you will choose "by hand", say you put the case 12 at 90deg et the case 0 and 24 a 0 degrees (or less) you can divide angle in equal parts.

     void Start () {
         DateTime dt = DateTime.Now;
         int hourOfTheDay = dt.Hour;
         Vector3 sunOrientation;
         
         switch (hourOfTheDay)
         {
         // Midnight
         case 0: sunOrientation = new Vector3 (...); break;
         
         // ...
         
         
         // Noon
         case 12: sunOrientation = new Vector3 (...); break;
     
        // ...
 
        // Midnight again
        case 24: sunOrientation = ... ; break;
         }
         
    }

Down there you can use Lerp function from the rotation you have selected to midnight at the speed you want

 void Update() {
         transform.rotation = Quaternion.Lerp(from.rotation, to.rotation, Time.time * speed);
     }



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

2 People are following this question.

avatar image avatar image

Related Questions

Help with Skybox Fading 1 Answer

How to make Scene darker to look more like night time. 1 Answer

Day/Nights cycle 2 Answers

DayNight controller script smoothed transition 0 Answers

Is there a way to make a day/night cycle (can be just light on/off) with a very high amount of lights? 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