How to do it...

To create a day and night cycle with the Animation View, follow these steps:

  1. Create an empty game object and call it DayAndNight.
  2. Create another empty game object, call it SunAndMoonPivot, and parent it to the DayAndNight game object.
  3. Create two more game objects, SunPivot and MoonPivot, and parent them to SunAndMoonPivot.
  4. Then create the Sun and Moon objects and parent the first one to SunPivot and the second one to MoonPivot.
  5. Lastly, create two Directional Lights and two Spheres and parent one of each to the Sun object and the remaining two to the Moon object.
  6. The hierarchy should look like the following:
    1. DayAndNightSunAndMoonPivot
    2. SunAndMoonPivotSunPivot
    3. SunAndMoonPivotMoonPivot
    4. SunPivotSun
    5. MoonPivotMoon
    6. SunDirectional Light
    7. SunSunSphere
    8. MoonDirectional Light
    9. MoonMoonSphere
  7. Create two materials: Moon and Sun. Choose the UnlitColor shader for them and adjust the colors to your liking (you can also use the Standard Shader with an Emission color).
  8. Apply the materials to the SunSphere and MoonSphere objects.
  9. Move the Sun and the Moon away from the center. You can also use the SunPivot and MoonPivot to rotate the Sun and the Moon. See the following screenshot for reference:
Sun and Moon placement
  1. Select the DayAndNight game object and add an Animator component to it.
  2. Create a new script and call it CameraColor.cs. This script has one public Color cameraColor variable. In the Update() function of this script, we set the backgroundColor of the main camera to the cameraColor variable's value:
        void Update () { 
            Camera.main.backgroundColor = cameraColor; 
        } 
  1. Assign the script to the DayAndNight game object.
  2. With the DayAndNight game object selected, open the Animation View.
  3. Create an animation of the Sun and Moon moving on the horizon. Rotate the SunAndMoonPivot to make the job easier.
  4. Animate the Directional Light colors and their Intensity and Shadow Strength. In the day, only the Sun's Directional Light should cast shadows, and in the night, only the Moon's Directional Light should cast shadows.
  5. If you are using the camera's background color as the background color of the scene, animate it using the public Color cameraColor of the script attached to SunAndMoonPivot game object. If you're using Unity's parametric skybox, make sure to choose your Sun's Directional Light as the Sun in the Lighting settings. This will make the skybox change with the respect of Sun's Directional Light rotation.
  6. Run the game to see the effect.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.201.95