Get the time of one revolution when rotating around an object?

Hi! I’m making a survival game, with a sun that’s rotating, and are going to change the temprature depending on which time of the day it is. To do this I want to get the revolution time of the sun, but I don’t know exactly how to write it in code. I’ve tried to calculate it with simple physics but it still gets wrong. The code I’m using for the rotation of the sun is:

transform.RotateAround(Vector3.zero, Vector3.right, rotationSpeed * Time.deltaTime);
transform.LookAt(Vector3.zero);

If you can help me with this or maby find a easier way I would be most greatful!

This is a bad approach in general. First of all you shouldn’t use RotateAround for things like that since it always performs a relative translation on a circular path. It’s prone to get off over time. Furthermore you would have to use the current location to reverse engineer the current time.

It’s way easier to use something like this:

  • create an empty gameobject at the world center.
  • Now simply parent your “sun”-gameobject to that empty gameobject
  • make sure the sun is at the very top. Simply set the local x and z coordinates of the sun to “0” and y to the desired distance / radius

Now just create a script which you attach to the empty gameobject which simply does something like that:

//C#
public float offset = 180f; // make sure time=0 equals midnight
public float time = 0f;
public float SecondsPerDay = 20f;

void Update()
{
    time += Time.deltaTime / SecondsPerDay;
    if (time > 1f) // wrap around after a day
        time -= 1f;
    transform.localRotation = Quaternion.Euler(time*360 + offset,0,0);
}

Now your sun will move accoring to the time. “time” goes from 0 to 1. So to compare it with out real time you could simply multiply time by 24 and you get 24 hours.

time   realtime
0      00:00
0.25   06:00
0.5    12:00
0.75   18:00
1      24:00 == 00:00

SecondsPerDay simply controls how long a day will take in seconds. if you want a day take as long as a real day you would need 246060 == 86400 seconds. If it should take 10 minutes you need 10*60 == 600 seconds.

If you want to know the current time in your game you can simple refer to your time variable. It dictates the current time.

Since the third parameter of that function is the angular change around the axis (as opposed to tangential) I would expect the time to do a full rotation would be 360 / rotationSpeed.