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;
}
}
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?
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.
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;
}
}
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!
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.
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
Follow this Question
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