0
\$\begingroup\$

I need help with some logic in my script. I created a dynamic day/night cycle that works fine in itself, but I want to add a game clock that works off the initial position of the sun (Directional Light).

I was thinking that it could be achieved by Shooting a ray from the directional light to the terrain and computing an angle which i would use to estimate the time.

  1. Would ray casting be the best/ most efficient way to do this?
  2. Is it even possible to achieve what I'm asking for, if not is there a method that better suits what I need?

So to go over.. I want the user to be able to place the sun anywhere along the x axis that would rotate around the terrain, when the user is satisfied with the position they chose, on startup of the game I would like my script to calculate the current position of the sun and then estimate the current time.

I can take care of the actual clock after that point but is ray casting from the sun to the terrain the way to go about something like this or should I be looking at this in totally different way?

\$\endgroup\$
8
  • \$\begingroup\$ Raycasts are good when you care about the intersection point. If all you care about is the angle, you could just use the rotation angle of the directional light, no? (If the angle at the intersection point with the terrain mattered, then it would be a different time of day on the ascending side of a hill than it is on the descending side...) \$\endgroup\$ – DMGregory Jun 16 '15 at 23:40
  • \$\begingroup\$ Thanks for the reply! i should have mentioned that the sun is attached to a sphere object that surrounds the terrain so its actually the sphere thats rotating which also makes the sun rotate around the terrain as well, so using something like eulerAngles.x in relation to the directional light would not suffice \$\endgroup\$ – davedno Jun 16 '15 at 23:44
  • \$\begingroup\$ A diagram or screengrab of your current setup would help then, annotated with what axes the user can modify. \$\endgroup\$ – DMGregory Jun 16 '15 at 23:47
  • \$\begingroup\$ You would get the Vector from Light.position to Terrain.position and calculate the angle between that and Vector3.up. Like @DMGregory mentioned, using Raycast would give you the angle between the Light and the triangle of the Terrain you hit which would point different directions depending on the shape of the terrain. \$\endgroup\$ – Jeremiah Leslie Jun 17 '15 at 1:05
  • \$\begingroup\$ I wrote something like this, I have the code at home though. Instead of setting the Time based on the position of the Sun, I would set the Sun based on the Time. I interpolate the required Angle based on the current time. You could do the reverse as well, however, I have no idea how to path your Sun's movement. If you could post some more information on your implementation it would help. Here is what mine looks like: i.stack.imgur.com/ZrAup.gif I can provide code later tonight. \$\endgroup\$ – jgallant Jun 17 '15 at 11:51
1
\$\begingroup\$

I have come up with a solution and it seemed to be buried deep within my college memory. I achieved what I wanted through a Unit Circle! Basic calculus/geometry seemed to be the correct way for me to come up with a working solution. I took the x and y translation points of the sun and put them through the unit circle equation. See https://www.mathsisfun.com/geometry/unit-circle.html for more information. Using these calculations I could base my logic off the degrees the equation gave me, giving me a working solution! Math is fun... Ish

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.