Day and Night Cycle rotation Problem

Hey guys,

I’m currently working on a day and night cycle for my project.
The problem is that I get some weird angles after I try to rotate my light.
Atm I calculate the passed seconds with the following formula:

float alreadyPassedSeconds = ((System.DateTime.Now.Hour * 60 * 60) + (System.DateTime.Now.Minute * 60) + System.DateTime.Now.Second);

After that I take that float and multiply it with the degrees per second:
DEGREES_PER_SECOND = 360 / SECONDSOFADAY; // SECONDSOFADAY = 86400
float startRotation = DEGREES_PER_SECOND * alreadyPassedSeconds;

And after that I simply rotate my sun/light on the x-axis to that value:
sun.transform.rotation = Quaternion.Euler(startRotation, 0, 0);

After I started my program at Hour: 16 Minute: 43 Second: 22 I get the startrotation of 250.8417.
So the values are fine but the rotation of the light is at 289.1565 and the scene is dark.

Where is my misconception?

using UnityEngine;

public class DayAndNightCicleScript : MonoBehaviour {
  public GameObject sun;

  private const float SECONDSOFADAY = 86400;
  private const float SECOND = 1;
  private const float MINUTE = 60 * SECOND;
  private const float HOUR = 60 * MINUTE;
  private const float DAY = 24 * HOUR;

  private const float DEGREES_PER_SECOND = 360 / SECONDSOFADAY;

  private float _SessionTime;

   // Use this for initialization
   void Start () {
  Debug.Log("Hour: " + System.DateTime.Now.Hour + " Minute: " + System.DateTime.Now.Minute + " Second: " + System.DateTime.Now.Second);
  float alreadyPassedSeconds = ((System.DateTime.Now.Hour * 60 * 60) + (System.DateTime.Now.Minute * 60)  + System.DateTime.Now.Second);
  float startRotation = DEGREES_PER_SECOND * alreadyPassedSeconds;
  Debug.Log("StartRotation: " + startRotation);
  sun.transform.rotation = Quaternion.Euler(startRotation, 0, 0);

  _SessionTime = 0;
  }

  // Update is called once per frame
  void Update() {
  sun.transform.Rotate(new Vector3(DEGREES_PER_SECOND, 0, 0) * Time.deltaTime);
  _SessionTime += Time.deltaTime;
   }
}

There seems to be a rash of issues with Euler angle lately. I never use them, myself, because they are sometimes counterintuitive. Try using Quaternion.AngleAxis instead. (Euler angles are just multiple AngleAxis calls really, only they don’t always rotate quite intuitively. I find it more natural to call AngleAxis several times and * the results together.)

(In particular, I can’t help but notice the mathematical symmetry of your chosen test value and its result. 289.1565 ~= 540 - 250.8417. I’d bet good money that (actual result) = 540 - (expected result) holds true for at least that “quadrant” of rotation and probably beyond, and similarly patterned for other quadrants as well. That’s basically an inversion as well as an “off by 180” error, both of which could be easily explained with unexpected Euler conversions.)

I suspect that your problem in this case is more with the setup of your light object than the script.

Hey,

thanks for the answer.
Now I tried:
sun.transform.rotation = Quaternion.AngleAxis(startRotation, Vector3.right);

But the result is the same. Hour: 12 Minute: 13 Second: 52 which would be an angle of 183.4667 somehow ‘evolves’ to 356.5318. I dont get it. My light is the standard light of a new generated scene but I think that none of it’s options has something to do with the axis… so what now?
I honestly don’t get it…

Try using a different axis (like .left, maybe?)

Doesn’t work.
I also tried adding or substracting of 180 but it also doesn’t work.
Tried something like this:

int counter = (int)startRotation;
        for(int i = 0; i < counter; i++)
        {
            sun.transform.Rotate(Vector3.right);
        }

still the same result.

I think you need an offset that is the time of day when the sun object’s rotation is zero. So in an empty scene, if I set the sun’s rotation to zeros, then I see that the light’s direction is heading down the z-axis. This roughly corresponds to 6 a.m. (If the light is pointing forward, it’s dawn ~6 a.m., if it’s straight down, it’s noon, etc.)

Here’s the additions to your script to account for this, I added the START_OF_DAY_OFFSET on line 13, and I’m using it on line 23:

using UnityEngine;

public class DayAndNightCicleScript : MonoBehaviour
{
    public GameObject sun;

    private const float SECONDSOFADAY = 86400f;
    private const float SECOND = 1f;
    private const float MINUTE = 60f * SECOND;
    private const float HOUR = 60f * MINUTE;
    private const float DAY = 24f * HOUR;

    private const float START_OF_DAY_OFFSET = 6f * HOUR;

    private const float DEGREES_PER_SECOND = 360f / SECONDSOFADAY;

    private float _SessionTime;

    // Use this for initialization
    void Start()
    {
        Debug.Log("Hour: " + System.DateTime.Now.Hour + " Minute: " + System.DateTime.Now.Minute + " Second: " + System.DateTime.Now.Second);
        float alreadyPassedSeconds = ((System.DateTime.Now.Hour * 60 * 60) + (System.DateTime.Now.Minute * 60)  + System.DateTime.Now.Second) - START_OF_DAY_OFFSET;
        float startRotation = DEGREES_PER_SECOND * alreadyPassedSeconds;
        Debug.Log("StartRotation: " + startRotation);
        sun.transform.rotation = Quaternion.Euler(startRotation, 0, 0);

        _SessionTime = 0;
    }

    // Update is called once per frame
    void Update()
    {
        sun.transform.Rotate(new Vector3(DEGREES_PER_SECOND, 0, 0) * Time.deltaTime);
        _SessionTime += Time.deltaTime;
    }
}

For me this seems to set the sun’s direction at around the right time for me (16 hours, 22 minutes). Hope this helps!

you can always get the position like this:
where center is de transform the script is on, and raduis the distance between your sun and the center

Vector3 UpdatePosition()
    {
        Vector3 position;

        float angle = (360f/86400)*alreadyPassedSeconds;
        float z = center.z+radius * Mathf.Sin(angle * Mathf.Deg2Rad);
        float y = center.y+radius * Mathf.Cos(angle * Mathf.Deg2Rad);
        position = new Vector3(center.x, y, z)+offset;
        return position;
    }

Hey jmjd,

I tried your code and it seems to work. Unfortunately in game mode the sun freezes at 90 degrees.
Here is another thing. In editor mode I can rotate the light around the x axis freely. In game mode I can rotate between 90 - 360 - 270 degrees. Sometimes I can rotate after 270 degrees and sometimes not.
That’s all really weird…

So Guys,

since I can’t get it to work I decided to cut this “feature”. This is my code now:

using UnityEngine;

public class DayAndNightCicleScript : MonoBehaviour
{
  public GameObject sun;

  private const float MORNING = 30F;
  private const float NOON = 90F;
  private const float EVENING = 170F;

  private int lastHour;
  private float _SessionTime;

  // Use this for initialization
  void Start()
  {
  setSun(System.DateTime.Now.Hour);
  _SessionTime = 0;
  }

  // Update is called once per frame
  void Update()
  {
  if(System.DateTime.Now.Hour != lastHour)
  {
  setSun(System.DateTime.Now.Hour);
  }
   
  _SessionTime += Time.deltaTime;
  }

  void setSun(int currentHour)
  {
  if (currentHour < 12 && currentHour > 6)
  {
  sun.transform.rotation = Quaternion.Euler(MORNING, 0, 0);
  }
  else if (currentHour < 18 && currentHour > 12)
  {
  sun.transform.rotation = Quaternion.Euler(NOON, 0, 0);
  }
  else if (currentHour < 24 && currentHour > 18)
  {
  sun.transform.rotation = Quaternion.Euler(EVENING, 0, 0);
  }
  else
  {
  sun.transform.rotation = Quaternion.Euler(360, 0, 0);
  }

  lastHour = currentHour;
  }
}

Had to get the values for the sun out of the game mode because there is a big difference between values of light in game and editor mode.
I hope that someday someone can bring clarity to this matter.

you can always try my free package (hopefully soon to be asset in the store) :slight_smile: [wip] Free Day/Night cycle - Community Showcases - Unity Discussions