Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by lpincombe · Feb 19, 2019 at 11:45 AM · rotationtimedirectional light

Rotating the Sun - why can't I do a simple rotation at a constant speed? The sun rotation falls out of sync with the time.

Just trying to get a constant rotation that I can speed up/slow down. I also have the current time displayed on screen.

For basics I just wanted a constant rotation, sun rises at 6, sets at 6. But as I increase the timespeed the hour + sunrise start to be off very slightly. This happens drastically at speed 4 and less noticably at speed 3 (at speed 3, the sun rises at 5am on the second day).

Any help? I'd ideally actually like to set the sun rise and set depending on the time. Cheers. Here's the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using System;
 
 public class TimeCycle : MonoBehaviour
 {
     private float secCount;
 
     private float secs;
     private int mins;
     private int hours;
     private int day;
 
     private string minsDisplay;
     private string hoursDisplay;
     private string secsDisplay;
 
     private float timeSpeed;       
 
     public Text TimeDisplay;
     public GameObject sun;
 
     private double sunSpeed = (1.0 / 240.0);
     
 
     
     void Start()
     {
         secCount = 0;
 
         secs = 0;
         mins = 0;
         hours = 6;
         day = 1;
 
         timeSpeed = 1;
 
         Debug.Log(sunSpeed);
                      
     }
 
     
     void Update()
     {
         TimeUpdate();
         SunUpdate();        
     }
 
     void TimeUpdate()
     {
         secs += Time.deltaTime * timeSpeed;
         
 
         if (secs >= 60)
         {
             mins += 1;
             secs = 0;
         }
             
         
 
         if (mins >= 60)
         {
             hours += 1;
             mins = 0;
         }
             
             
         if (hours >= 24)
         {
             day += 1;
             secs = 0;
             hours = 0;
         }
 
         if (day >= 366)
         {
             day = 1;
         }
 
         if (mins < 10)
         {
             minsDisplay = "0" + mins.ToString();
         }
         else
         {
             minsDisplay = mins.ToString();
         }
 
         if (hours < 10)
         {
             hoursDisplay = "0" + hours.ToString();
         }
         else
         {
             hoursDisplay = hours.ToString();
         }
 
         if (secs < 10)
         {
             secsDisplay = "0" + Math.Floor(secs).ToString();
         }
         else
         {
             secsDisplay = Math.Floor(secs).ToString();
         }
 
                        
             TimeDisplay.text = "Day: " + day.ToString() + "     Time: " + hoursDisplay + ":" + minsDisplay + ":" + secsDisplay;
         
      
     }
 
     void SunUpdate()
     {        
         
         sun.transform.Rotate((float)sunSpeed * Time.deltaTime, 0, 0);       
 
                
     }
 
     public void PauseTime()
     {
         timeSpeed = 0;
         sunSpeed = 0;
     }
 
 
     public void Speed1()
     {
         timeSpeed = 1;
         sunSpeed = (1.0 / 240.0);        
     }
 
     public void Speed2() //1 min per second
     {
         timeSpeed = 60;
         sunSpeed = (1.0 / 240.0) * timeSpeed;        
     }
 
     public void Speed3() //10 mins per second
     {
         timeSpeed = 600;
         sunSpeed = (1.0 / 240.0) * timeSpeed;        
     }
 
     public void Speed4() //15 mins per second
     {
         timeSpeed = 900;
         sunSpeed = (1.0 / 240.0) * timeSpeed;       
     }
 }
 
 
Comment
Add comment · Show 2
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 lpincombe · Feb 19, 2019 at 01:53 PM 0
Share

I'm now trying to rotate the sun like so:

 var step = (float)sunSpeed * Time.deltaTime;
 
         if (sun.transform.localEulerAngles.x < 180 && sun.transform.rotation.x >= 0)
         {
             sun.transform.rotation = Quaternion.RotateTowards(sun.transform.rotation, sunset.rotation, step);
         } else
         {
             sun.transform.rotation = Quaternion.RotateTowards(sun.transform.rotation, sunrise.rotation, step);
         } 


But the rotation gets stuck at -180. The sun rotates during the day towards sunset fine but at sunset get stuck. Why?

avatar image jespercal · Feb 19, 2019 at 03:05 PM 0
Share

If you change the sun rotation to something like this:

 sun.transform.rotation = Quaternion.Euler(new Vector3((hours*15)-90, 0,0));

Then it will rise at 6 and fall at 18, then restart the next day. It's very chunky however, so I would maybe count a whole day in either $$anonymous$$utes or even better, seconds, and then divide it into the different $$anonymous$$utes, hours display.

2 Replies

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

Answer by jespercal · Feb 19, 2019 at 03:50 PM

Here's the script with the sun rotation using seconds. I added comments to explain the different things.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using System;
 
 public class TimeCycle : MonoBehaviour
 {
     private float secCount;
 
     private float secs;
     private int mins;
     private int hours;
     private int day;
 
     private string minsDisplay;
     private string hoursDisplay;
     private string secsDisplay;
 
     private float timeSpeed;
 
     public Text TimeDisplay;
     public GameObject sun;
 
 
 
     void Start()
     {
         secCount = 0;
 
         secs = ((60)*60)*14; // With this, the time will start at 6.
         day = 1;
 
         timeSpeed = 1;
     }
 
 
     void Update()
     {
         TimeUpdate();
         SunUpdate();
         Speed1();
     }
 
     void TimeUpdate()
     {
         secs += Time.deltaTime * timeSpeed;
 
         secsDisplay = ((int)secs % 60).ToString(); // We take the seconds and use the Modulus operator to make it loop from 0 to 59 and then resets, without actually resetting the seconds.
         secsDisplay = secsDisplay.Length < 2 ? "0" + secsDisplay : secsDisplay; // If it's only one letter long (like "5"), then it'll add a zero "05"
         mins = ((int)secs / 60) % 60; // Same as with seconds, but we divide it by 60 to get minutes first.
         minsDisplay = mins.ToString();
         minsDisplay = minsDisplay.Length < 2 ? "0" + minsDisplay : minsDisplay;
 
         hours = (((int)secs / 60)/60);
         hoursDisplay = hours.ToString();
         hoursDisplay = hoursDisplay.Length < 2 ? "0" + hoursDisplay : hoursDisplay;
 
         if (hours >= 24)
         {
             day += 1;
             secs = 0; // When a day has elapsed, we can reset the seconds.
         }
 
         TimeDisplay.text = "Day: " + day.ToString() + "     Time: " + hoursDisplay + ":" + minsDisplay + ":" + secsDisplay;
     }
 
     void SunUpdate()
     {
         sun.transform.rotation = Quaternion.Euler(new Vector3((((secs / 60)/60)*15)-90, 0,0)); // It looks like a mess and can probably be changed to look better, but it takes seconds and divides it into 24 hours, then times 15 to get 360 degrees.
     }
 
     public void PauseTime()
     {
         timeSpeed = 0;
     }
 
 
     public void Speed1()
     {
         timeSpeed = 1;
     }
 
     public void Speed2() //1 min per second
     {
         timeSpeed = 60;
     }
 
     public void Speed3() //10 mins per second
     {
         timeSpeed = 600;
     }
 
     public void Speed4() //15 mins per second
     {
         timeSpeed = 900;
     }
     public void Speed5() //30 mins per second
     {
         timeSpeed = 1800;
     }
     public void Speed6() //1 hour per second - Just to test
     {
         timeSpeed = 3600;
     }
 }
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 lpincombe · Mar 07, 2019 at 11:01 AM 0
Share

Thanks for this, sorry for not getting back to you sooner. $$anonymous$$y main issue ended up being that I was relying on two time.deltaTimes, ins$$anonymous$$d of basing the sun's rotation on the already established seconds count. But there are a few helpful things in here I didn't know how to do (as neatly/succinctly as you have) so thank you!

avatar image
0

Answer by lpincombe · Feb 19, 2019 at 03:24 PM

Can you explain where you get those numbers from? Why the hours * 15, why -90? Thank you.

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 lpincombe · Feb 19, 2019 at 03:28 PM 0
Share

Actually, I think I have figured it out. I just need to change those numbers so they work with seconds rather than hours.

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

201 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I want to make a script that causes the directional light to stop at 180 degrees. 0 Answers

Rotation faster on lower fps,rotation sensitifity 1 Answer

How to Lock rotation for few seconds only when every time the object rotates 180 degree? 1 Answer

Pausing a quaternion back and forth sin rotation based on time.time 1 Answer

What is the most accurate way to call a function based on time? 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