Hour 5 - The Sun Appears!

Created a simple Sun object which has a directional light attached and orbits around the island to simulate a basic day/night cycle effect. The player can also grab the sun and move it around manually, as well as "fling" it around. Just for fun!

Integrated the free LeanTween plugin to smooth out the "fling" interpolation. Nice and lightweight and really easy to use straight out of the box. Should continue to come in handy for future polish. Also added a CameraController with zooming via the scroll wheel.

Some Technical Notes

It took longer than expected to get the orbital rotation and related input logic working nicely because MATHS kept tripping me up. The final code ended up very simple, but I took a lot of detours getting there! 

Unity's RotateAround achieves the basic orbiting, nice 'n' easy. Manually moving the sun is achieved by casting a ray into the scene from the cursor position and then finding the nearest point on the XZ plane along the sun's orbital path and snapping the sun straight there.

I tried doing this manually for a while but eventually got better results by just bunging in a great big invisible plane object on the XZ plane (on its own layer) that I could cast against directly. 

When I get a hit against the plane I calculate the direction vector from the center of the island/scene/world to that point, normalise it, and then multiply by the sun's standard distance i.e. the radius of the sun's orbit. And voila, a valid orbital position for our sun.

Flinging the sun proved tricky due to having to work out which way to fling. Depending on which part of the "sky" the sun is in affects what input should translate to a clockwise or anti-clockwise rotation. I eventually used the change in angle (relative to the up axis) when the sun is being dragged to determine direction, and then adjusted the "fling" velocity accordingly.

Get Plantasia

Leave a comment

Log in with your itch.io account to leave a comment.