Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Bengooch7 · Jul 20, 2015 at 04:42 PM · javascriptlightingtimealgorithmdaycycle

Real-time day/night system breaks after noon

I've created a day/night system that basically rotates a directional light at a rate that would be realistic to the real life passage of time. It also calculates the position the "sun" should start at based on the current system time. This script was working beautifully this morning, and I tried it a few minutes before noon today and everything still worked.

After 12:00pm, when I run the game it thinks that it is night time. Not sure why, as the calculations used to decide where the sun should be is based on how many seconds have passed since 00:00 (midnight). For example, if the time is 12:27pm it takes the number of hours and figures out how many seconds that would be (12*60*60) and adds that to the number of minutes converted to seconds (27*60) then adds in the current number of seconds (say 54) and multiplies all that by the rate of rotation (0.004166667 degrees per second) then sets the rotation to that position.

That means that it should be set to (0,0,186.975014958) but it is instead setting to around 55.91.

Any ideas??

 #pragma strict
 
 public var normalPassageOfTime = true;
 
 //How many degrees the sun rotates per second
 private var degreesOfRotation : float = 360f /*degrees*/ / 24f /*hours*/ / 60f /*minutes*/ / 60f /*seconds*/;
 
 //Visible variable to adjust speed of time flow
 public var speedOfLight : float = 1f;
 
 //Calculation of time flow speed
 private var speedModifier : float;
 
 //Get Hours, Minutes, and Seconds as a string
 public var currentHour = System.DateTime.Now.ToString("HH");
 public var currentMin = System.DateTime.Now.ToString("mm");
 public var currentSec = System.DateTime.Now.ToString("ss");
 
 //Convert current number of hours/minutes passed since midnight into usable seconds
 //Seconds will just equal current as a float
 private var hoursInSeconds : float = parseFloat(currentHour) * 60f * 60f;
 private var minutesInSeconds : float = parseFloat(currentMin) * 60f;
 private var secondsInSeconds : float = parseFloat(currentSec);
 
 function Awake() {
 //set position of the sun based on time of day
     transform.Rotate(0,0,degreesOfRotation * (hoursInSeconds + minutesInSeconds + secondsInSeconds));
     
 
 }
 
 function Start () {
     
     
 }
 
 function Update () {
     //rotate the sun based on set speed (Needs Variables adjusted)
     if (normalPassageOfTime == true) {
         transform.Rotate(0,0,speedModifier * Time.deltaTime);
         //Multiplies speed variable by number of degrees to rotate per second
         speedModifier = degreesOfRotation * speedOfLight;
         }
     
 }
 
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 Bengooch7 · Jul 20, 2015 at 06:50 PM 0
Share

Ladies and gentlemen, I've figured out my "Why". It seems that I had adjusted the rotation of the light between when it was working and when it stopped working. Apparently, the way I have it written, the algarithem just adds the number of degrees to the current rotation. Is there a way to normalize the rotation to (0,0,0) first, or to have it set the rotation to exactly what is in the script ins$$anonymous$$d of having it add on the degrees?

Thanks

1 Reply

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

Answer by alexi123454 · Jul 20, 2015 at 07:09 PM

So as you said in your comment, transform.Rotate will actually add a rotation of that many degrees around the axis, rather than just setting its rotation. There are a number of simple ways to get around that, the simplest of which is to set the object's rotation to Quaternion.identity before doing the transform.Rotate function. Another simple way is to add rotations, which can be done by multiplying the quaternions: transform.rotation = (Quaternion.Euler(0,0,0) * Quaternion.Euler(0,0,newRotation)) will give you a rotation of 0, and then a rotation around the z axis of newRotation.

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 Bengooch7 · Jul 20, 2015 at 08:20 PM 0
Share

$$anonymous$$an, That is super helpful! I haven't tried this yet, but I will later today. I don't know what the Quaternion business is, or how it works, but I'll just throw it in there and do a little research on it so I can be sure I understand how my own script functions.

Thank you!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I show the time in this Day/Night Cycle Script 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

day/cycle blending and fading 1 Answer

how to stop every thing and enter into the OnTriggerEnter function? 0 Answers

Plain calculating operations? :o 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