0
\$\begingroup\$

I'm working on a game in which the season is particularly important to the player, along with sunlight. It progresses from early spring to late fall. I have a very basic day/night cycle system working, but dawn and dusk are consistently at the same times; whereas, in the real world, days get longer into the summer and shorter into the winter.

The whole thing basically involves rotating a directional light now; but aside from changing the rate of rotation of the light in a non-linear fashion, I'm not sure how to implement the day-length-per-day-of-year system. Before I do something like that, I can't help but feel like there must be a better way to do it.

In summary, if I haven't been clear, sunrise and sunset are 6AM and 6PM (mapped to a normalized angle) by default. I may need sunrise at 5AM and sunset as late as 9PM at some point in my game. Is there an efficient or built-in way of managing this?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Unity does not do dawn or dusk, that's a concept introduced by your game, not the engine, so it has no default. It sounds like what you want is to calculate the position of the sun in the sky, based on time of day/year, so you can shine the light from that angle. This is a question for the Astronomy StackExchange, where it has already been asked. Users there linked to this Wikipedia article outlining the steps. \$\endgroup\$
    – DMGregory
    Oct 30, 2022 at 15:02
  • \$\begingroup\$ @DMGregory An excellent point! Thank you for the link, it did feel like I was missing something overt. I'm willing to close this question, and wish I could give you credit for the solution. \$\endgroup\$ Oct 30, 2022 at 15:32
  • 1
    \$\begingroup\$ You can click the delete link if you no longer need help with this. Or, if you are able to apply the advice found via astronomy sources to a working solution for your game, you could post that solution as an answer to help other devs who also want to implement time of day features. \$\endgroup\$
    – DMGregory
    Oct 30, 2022 at 15:39
  • \$\begingroup\$ @DMGregory I'm thinking the second one, in the spirit of stack exchange; but first I need to actually implement it in practice, which might take a little time. Again, thank you for the help. \$\endgroup\$ Oct 30, 2022 at 15:54

1 Answer 1

1
\$\begingroup\$

So you probably need some sort of "Calendar"... maybe a simple range tracked from 0 - 365. Track the current day in a variable and increment it each new day. We'll also need some sort of light value. Let's say from 50% light to 100% light.

To adjust the lighting, we're going to assume that summer has the most sun, and work backwards from there. So you'll want to map the current day of the year to a percentage of light. To simplify things, let's assume that January 1st (Day 0 or 1) is the winter solstice with the least amount of light. So we would map 0 to 50%. July 1st (day 180) would be about 100%, and then back to December 31st (day 365) at 50%.

From there, you can apply that light percentage in 2 ways. The speed at which the light rotates, and the height of the light in the sky. 50% light means that the light should rotate faster. 100% light would mean normal speed. 50% light would also mean the light is lower in the sky, just like in the winter the sun is lower in the sky. 100% light would mean that the light would be directly overhead, or almost directly overhead.

One suggestion: Depending on your setup, you may not want to just rotate the light, but move the light across the map. This should cause the shadows to move and provide a really cool affect where locations look entirely different in the morning vs the night, even though the have the same amount of light.

\$\endgroup\$
6
  • \$\begingroup\$ Your hypothesis is that the Earth's rotation speeds up in some seasons and slows down in others? It's an interesting idea, but I'm not sure how realistic it is... 😉 \$\endgroup\$
    – DMGregory
    Nov 2, 2022 at 14:53
  • \$\begingroup\$ @DMGregory This is actually pretty close to what I'm doing; but I'm going to be tilting the axis of the light to change the position in the sky. What I'm thinking more than altering the speed directly, is altering the amount of time it takes from the sun to go from the rise point (the dawn hour) to the set point (dusk hour) each day of the calendar. The problem is that my space is truly flat, with a light at infinity; whereas the real world is almost spherical with a far, but not infinite, sun; so we gotta emulate that. \$\endgroup\$ Nov 2, 2022 at 17:17
  • \$\begingroup\$ Hint: what actually changes the length of the day in real life is a change in where the sun rises and sets, so it crosses a larger or shorter arc across the sky, NOT a change in how fast the Earth spins / how fast the sun moves. The finite distance to the sun is not a relevant consideration here - it's far enough away that treating it as infinitely distant is a sufficiently close approximation. \$\endgroup\$
    – DMGregory
    Nov 2, 2022 at 17:22
  • \$\begingroup\$ So I'm not trying to say that the real earth speeds up and slows down... but I'm guessing his playing area is a relatively flat surface... in which I think we would need to simulate the fact that the sun is lower in the sky. We don't have a flat earth so the sphere shape affects (I think) how the sunlight hits it. So simply lowering the light in the sky wont complete the "illusion" of a round earth. Or maybe you could fade in/out the light based on that percentage as well. \$\endgroup\$
    – John B
    Nov 3, 2022 at 12:16
  • \$\begingroup\$ If you are dealing with a spherical playing area, then it's probably good enough to just change the rotation/position and not the speed. \$\endgroup\$
    – John B
    Nov 3, 2022 at 12:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .