Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by leoufdetou · Jan 07, 2012 at 02:30 AM · controllernightday

DayNight controller script smoothed transition

Hello, My question is a simple question about a little script which allowed user to make a Day/Night cycles.It's works perfectly but the part of the code which allowed user to make a smoothed skybox transition.I read the code again and again but found no error :/ If anyone find mistakes,this guy would be awesome :D First part is only commentary,so read them only if you want ^^ It's a big code which you can se here(without the smoothed skybox transition):Unify community script DayNight Controller

Here is the code:

 using UnityEngine;
 using System.Collections;


  //Implements a Day/Night cycle relative to the game world, with a World-Time   //clock, and optional Direcitonal Light control.

 //Add this script to a new GameObject to create a Day/Night cycle for the scene. The //day/night cycle effect is achieved by modifying the
 //scene ambient light color, fog color, and skybox material.  The script will also  //rotate, fade, and enable/disable a directional
 //light if one is attached to the same GameObject as the DayNightController        //script.  The length of a complete day (in seconds) and the number of
 //hours per day are modifiable in the script fields and allow calculation of the                
 //World-time hour-of-day.  Each 'phase' of the day is considered
 //to be 1/4 of the dayCycleLength.

 //Note that the script will rotate the GameObject transform it is attached to,     even if no directional light is attached. You will probably want to
 //use a dedicated GameObject for this script and any attached directional light.

 //The GameObject with this script should be placed roughly in the center of your scene, with a height of about 2/3 the length (or width) of the terrain.
 //If that GameObject has a light, it should be a directional light pointed straight down (x:90, y:0, z:0).  This light will then be rotated around its
 //x-axis (relative to the scene; eg. as if you used the rotation tool locked of the green 
 //x axis) and will reach its horizontal peeks during the
 //end of dusk and beginning of dawn, turning off during the night (upside-down rotation).
 //The reset command will attempt to use the default skybox assets DawnDusk, Sunny2, and StarryNight if that package has been imported.  The
 //command will also choose acceptable color values and set the day cycle to two minutes. It is suggested that the directional light be a light-
 //yellow or peach in color with a roughly 0.33f intensity.  The script will not set any default values for the light, if one exists, so the light
 //must be configured manually.

 public class DayNightControllerSmoothed : MonoBehaviour

{

 //The number of real-world seconds in one game day.
 public float dayCycleLength;
 
 
 // The current time within the day cycle. Modify to change the World Time.
 
 public float currentCycleTime;
   
 //Would be the amount of time the sky takes to transition if UpdateSkybox were used.
 public float skyTransitionTime;

 /// <summary>
 /// The current 'phase' of the day; Dawn, Day, Dusk, or Night
 /// </summary>
 public DayPhase currentPhase;
   
 /// <summary>
 /// The number of hours per day used in the WorldHour time calculation.
 /// </summary>
 public float hoursPerDay;

 /// <summary>
 /// Dawn occurs at currentCycleTime = 0.0f, so this offsets the WorldHour time to make
 /// dawn occur at a specified hour. A value of 3 results in a 5am dawn for a 24 hour world clock.
 /// </summary>
 public float dawnTimeOffset;
   
 /// <summary>
 /// The calculated hour of the day, based on the hoursPerDay setting. Do not set this value.
 /// Change the time of day by calculating and setting the currentCycleTime.
 /// </summary>
 public int worldTimeHour;

 /// <summary>
 /// The scene ambient color used for full daylight.
 /// </summary>
 public Color fullLight;

 /// <summary>
 /// The scene ambient color used for full night.
 /// </summary>
 public Color fullDark;

 /// <summary>
 /// The scene skybox material to use at dawn and dusk.
 /// </summary>
 public Material dawnSkybox;
 
 public Color dawnFog;
 /// <summary>
 /// The scene fog color to use at dawn and dusk.
 /// </summary>
 
 

 /// <summary>
 /// The scene skybox material to use during the day.
 /// </summary>
 public Material daySkybox;

 /// <summary>
 /// The scene fog color to use during the day.
 /// </summary>
 public Color dayFog;
 public Material duskSkybox;
 public Color duskFog;
 /// <summary>
 /// The scene skybox material to use at night.
 /// </summary>
 public Material nightSkybox;

 /// <summary>
 /// The scene fog color to use at night.
 /// </summary>
 public Color nightFog;

 /// <summary>
 /// The calculated time at which dawn occurs based on 1/4 of dayCycleLength.
 /// </summary>
 private float dawnTime;

 /// <summary>
 /// The calculated time at which day occurs based on 1/4 of dayCycleLength.
 /// </summary>
 private float dayTime;

 /// <summary>
 /// The calculated time at which dusk occurs based on 1/4 of dayCycleLength.
 /// </summary>
 private float duskTime;

 /// <summary>
 /// The calculated time at which night occurs based on 1/4 of dayCycleLength.
 /// </summary>
 private float nightTime;

 /// <summary>
 /// One quarter the value of dayCycleLength.
 /// </summary>
 private float quarterDay;

 //Would be the amount of time remaining in the skybox transition if UpdateSkybox were used.
 private float remainingTransition;
   
 /// <summary>
 /// The specified intensity of the directional light, if one exists. This value will be
 /// faded to 0 during dusk, and faded from 0 back to this value during dawn.
 /// </summary>
 private float lightIntensity;

 /// <summary>
 /// Initializes working variables and performs starting calculations.
 /// </summary>
 void Initialize()
 {
     remainingTransition = skyTransitionTime; //Would indicate that the game should start with an active transition, if UpdateSkybox were used.
     quarterDay = dayCycleLength * 0.25f;
     dawnTime = 0.0f;
     dayTime = dawnTime + quarterDay;
     duskTime = dayTime + quarterDay;
     nightTime = duskTime + quarterDay;
     if (light != null)
     { lightIntensity = light.intensity; }
 }

 /// <summary>
 /// Sets the script control fields to reasonable default values for an acceptable day/night cycle effect.
 /// </summary>
 void Reset()
 {
     dayCycleLength = 120.0f;
     skyTransitionTime = 3.0f; //would be set if UpdateSkybox were used.
     hoursPerDay = 24.0f;
     dawnTimeOffset = 3.0f;
     fullDark = new Color(32.0f / 255.0f, 28.0f / 255.0f, 46.0f / 255.0f);
     fullLight = new Color(253.0f / 255.0f, 248.0f / 255.0f, 223.0f / 255.0f);
     dawnFog = new Color(133.0f / 255.0f, 124.0f / 255.0f, 102.0f / 255.0f);
     duskFog = new Color(133.0f / 255.0f, 124.0f / 255.0f, 102.0f / 255.0f);
     dayFog = new Color(180.0f / 255.0f, 208.0f / 255.0f, 209.0f / 255.0f);
     nightFog = new Color(12.0f / 255.0f, 15.0f / 255.0f, 91.0f / 255.0f);
     Skybox[] skyboxes = AssetBundle.FindObjectsOfTypeIncludingAssets(typeof(Skybox)) as Skybox[];
     foreach (Skybox box in skyboxes)
     {
         if (box.name == "Dawn Skybox")
         { dawnSkybox = box.material; }
         if(box.name == "duskSkybox")
         { duskSkybox = box.material;}
         else if (box.name == "StarryNight Skybox")
         { nightSkybox = box.material; }
         else if (box.name == "Sunny2 Skybox")
         { daySkybox = box.material; }
     }
 }

 // Use this for initialization
 void Start()
 {
     Initialize();
 }

 // Update is called once per frame
 void Update()
 {
     // Rudementary phase-check algorithm:
     if (currentCycleTime > nightTime && currentPhase == DayPhase.Dusk)
     {
         SetNight();
     }
     else if (currentCycleTime > duskTime && currentPhase == DayPhase.Day)
     {
         SetDusk();
     }
     else if (currentCycleTime > dayTime && currentPhase == DayPhase.Dawn)
     {
         SetDay();
     }
     else if (currentCycleTime > dawnTime && currentCycleTime < dayTime && currentPhase == DayPhase.Night)
     {
         SetDawn();
     }

     // Perform standard updates:
     UpdateWorldTime();
     UpdateDaylight();
     UpdateFog();
     UpdateSkybox(); //would be called if UpdateSkybox were used.

     // Update the current cycle time:
     currentCycleTime += Time.deltaTime;
     currentCycleTime = currentCycleTime % dayCycleLength;
 }

 /// <summary>
 /// Sets the currentPhase to Dawn, turning on the directional light, if any.
 /// </summary>
 public void SetDawn()
 {
     //RenderSettings.skybox = dawnSkybox; //would be commented out or removed if UpdateSkybox were used.
     remainingTransition = skyTransitionTime; //would be set if UpdateSkybox were used.
     if (light != null)
     { light.enabled = true; }
     currentPhase = DayPhase.Dawn;
 }

 /// <summary>
 /// Sets the currentPhase to Day, ensuring full day color ambient light, and full
 /// directional light intensity, if any.
 /// </summary>
 public void SetDay()
 {
     //RenderSettings.skybox = daySkybox; //would be commented out or removed if UpdateSkybox were used.
     remainingTransition = skyTransitionTime; //would be set if UpdateSkybox were used.
     RenderSettings.ambientLight = fullLight;
     if (light != null)
     { light.intensity = lightIntensity; }
     currentPhase = DayPhase.Day;
 }

 /// <summary>
 /// Sets the currentPhase to Dusk.
 /// </summary>
 public void SetDusk()
 {
     //RenderSettings.skybox = duskSkybox; 
     //would be commented out or removed if UpdateSkybox were used.
     remainingTransition = skyTransitionTime; //would be set if UpdateSkybox were used.
     currentPhase = DayPhase.Dusk;
 }

 /// <summary>
 /// Sets the currentPhase to Night, ensuring full night color ambient light, and
 /// turning off the directional light, if any.
 /// </summary>
 public void SetNight()
 {
     //RenderSettings.skybox = nightSkybox; //would be commented out or removed if UpdateSkybox were used.
     remainingTransition = skyTransitionTime; //would be set if UpdateSkybox were used.
     RenderSettings.ambientLight = fullDark;
     if (light != null)
     { light.enabled = false; }
     currentPhase = DayPhase.Night;
 }

 /// <summary>
 /// If the currentPhase is dawn or dusk, this method adjusts the ambient light color and direcitonal
 /// light intensity (if any) to a percentage of full dark or full light as appropriate. Regardless
 /// of currentPhase, the method also rotates the transform of this component, thereby rotating the
 /// directional light, if any.
 /// </summary>
 private void UpdateDaylight()
 {
     if (currentPhase == DayPhase.Dawn)
     {
         float relativeTime = currentCycleTime - dawnTime;
         RenderSettings.ambientLight = Color.Lerp(fullDark, fullLight, relativeTime / quarterDay);
         if (light != null)
         { light.intensity = lightIntensity * (relativeTime / quarterDay); }
     }
     else if (currentPhase == DayPhase.Dusk)
     {
         float relativeTime = currentCycleTime - duskTime;
         RenderSettings.ambientLight = Color.Lerp(fullLight, fullDark, relativeTime / quarterDay);
         if (light != null)
         { light.intensity = lightIntensity * ((quarterDay - relativeTime) / quarterDay); }
     }

     transform.Rotate(Vector3.right * ((Time.deltaTime / dayCycleLength) * 360.0f), Space.Self);

}

 /// <summary>
 /// Interpolates the fog color between the specified phase colors during each phase's transition.
 /// eg. From DawnDusk to Day, Day to DawnDusk, DawnDusk to Night, and Night to DawnDusk
 /// </summary>
 private void UpdateFog()
 {
     if (currentPhase == DayPhase.Dawn)
     {
         float relativeTime = currentCycleTime - dawnTime;
         RenderSettings.fogColor = Color.Lerp(dawnFog, dayFog, relativeTime / quarterDay);
     }
     else if (currentPhase == DayPhase.Day)
     {
         float relativeTime = currentCycleTime - dayTime;
         RenderSettings.fogColor = Color.Lerp(dayFog, duskFog, relativeTime / quarterDay);
     }
     else if (currentPhase == DayPhase.Dusk)
     {
         float relativeTime = currentCycleTime - duskTime;
         RenderSettings.fogColor = Color.Lerp(duskFog, nightFog, relativeTime / quarterDay);
     }
     else if (currentPhase == DayPhase.Night)
     {
         float relativeTime = currentCycleTime - nightTime;
         RenderSettings.fogColor = Color.Lerp(nightFog, dawnFog, relativeTime / quarterDay);
     }
 }

 //Not yet implemented, but would be nice to allow a smoother transition of the Skybox material.
 private void UpdateSkybox()
 {
     if (remainingTransition > 0.0f)
     {
         if (currentPhase == DayPhase.Dawn)
         {
             RenderSettings.skybox.Lerp(dawnSkybox, nightSkybox, remainingTransition / skyTransitionTime);
         }
         if (currentPhase == DayPhase.Day)
         {
            RenderSettings.skybox.Lerp(nightSkybox,daySkybox, remainingTransition / skyTransitionTime);
         }
         if (currentPhase == DayPhase.Dusk)
         {
            RenderSettings.skybox.Lerp(daySkybox,duskSkybox, remainingTransition / skyTransitionTime);
         }
         if (currentPhase == DayPhase.Night)
         {
            RenderSettings.skybox.Lerp(duskSkybox,nightSkybox, remainingTransition / skyTransitionTime);
         }
        remainingTransition -= Time.deltaTime;
     }
 }

 /// <summary>
 /// Updates the World-time hour based on the current time of day.
 /// </summary>
 private void UpdateWorldTime()
 {
     worldTimeHour = (int)((Mathf.Ceil((currentCycleTime / dayCycleLength) * hoursPerDay) + dawnTimeOffset) % hoursPerDay) + 1;
 }

 public enum DayPhase
 {
     Night = 0,
     Dawn = 1,
     Day = 2,
     Dusk = 3
 }

}

It's the end :D It's a bit long,so I will understand if I get no answer... I hope you could hlp me and thanks anyway :D

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IAMBATMAN · Aug 29, 2016 at 01:30 PM 0
Share

BU$$anonymous$$P!! I need this right about now, I'm using the script from here http://wiki.unity3d.com/index.php/DayNightController

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Day/Nights cycle 2 Answers

Help with Skybox Fading 1 Answer

How to make Scene darker to look more like night time. 1 Answer

Lightmaps for night and day 0 Answers

Day And Night Clock issue 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges