# Would Raycast suffice for a game clock based on sun position?

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?

• 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...) Commented Jun 16, 2015 at 23:40
• 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 Commented Jun 16, 2015 at 23:44
• A diagram or screengrab of your current setup would help then, annotated with what axes the user can modify. Commented Jun 16, 2015 at 23:47
• 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. Commented Jun 17, 2015 at 1:05
• 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.sstatic.net/ZrAup.gif I can provide code later tonight. Commented Jun 17, 2015 at 11:51