Search Unity

Unisky realisitc day night cycle + random weather

Discussion in 'Scripting' started by Zastrow89, Aug 21, 2011.

  1. Zastrow89

    Zastrow89

    Joined:
    May 1, 2010
    Posts:
    51
    anyone who got a script that will do as written above? make unisky have a really nice random weather and day night cycle? what i got this far is
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleScript : MonoBehaviour {
    5.  
    6. // create an instance of the API
    7. private UniSkyAPI uniSky;
    8.  
    9. void Start() {
    10.  
    11. // define the instance
    12. uniSky = GameObject.Find("UniSkyAPI").GetComponent("UniSkyAPI") as UniSkyAPI;
    13.  
    14. // instantiate
    15. uniSky.InstantiateUniSky();
    16. }
    17.  
    18. void Update() {
    19.  
    20. // set up a 24-hour cycle with a framerate dependent speed of 1.0
    21. uniSky.SetTime(uniSky.GetTime() + Time.deltaTime * 1.0f);
    22.     }
    23. }
    24.  
    thanks alot in advance
     
  2. xshad0wfx

    xshad0wfx

    Joined:
    Sep 26, 2011
    Posts:
    1
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class UniskyGameTime : MonoBehaviour {
    6.    
    7.     // instance of the API
    8.     private UniSkyAPI uniSky;
    9.    
    10.     public float dayCycleInMinutes = 1; //How many minutes in real time a game day will pass
    11.    
    12.    
    13.     private float _dayCycleInSeconds;
    14.    
    15.     private const float SECOND = 1;
    16.     private const float MINUTE = 60  * SECOND;
    17.     private const float HOUR = 60 * MINUTE;
    18.     private const float DAY = 24 * HOUR;
    19.    
    20.    
    21.    
    22.     private float _dayPercent = 0;
    23.    
    24.     private float _timeOfDay;
    25.    
    26.  
    27.     // Use this for initialization
    28.     void Awake () {
    29.        
    30.        
    31.         _dayCycleInSeconds = dayCycleInMinutes * MINUTE;
    32.        
    33.        
    34.        
    35.         // Define instance
    36.         uniSky = GameObject.Find("UniSkyAPI").GetComponent("UniSkyAPI") as UniSkyAPI;
    37.        
    38.         // Initiate and create default UniSky
    39.         uniSky.InstantiateUniSky();
    40.        
    41.         // Set some initial states
    42.         uniSky.SetTime(12.0f);
    43.         uniSky.SetAmbientLighting(new Color(0.1f, 0.1f, 0.1f, 0.1f));
    44.         uniSky.SetStormCenter(new Vector3(0,0,0));
    45.         uniSky.SetSunShadows(LightShadows.Soft);
    46.        
    47.         }
    48.        
    49.  
    50.        
    51.    
    52.    
    53.     // Update is called once per frame
    54.     void Update () {
    55.        
    56.         _timeOfDay += Time.deltaTime;
    57.         _dayPercent = _timeOfDay / _dayCycleInSeconds * 24;
    58.         if (_timeOfDay > _dayCycleInSeconds)
    59.         {
    60.        
    61.             _timeOfDay -= _dayCycleInSeconds;
    62.         }
    63.         uniSky.SetTime(_dayPercent);
    64.        
    65.         Debug.Log(_timeOfDay);
    66.        
    67.        
    68.     }
    69. }
    70.  
    Here's a start for you. It allows you to control how long your in game days in real time minutes. It changes from day to night accordingly starting at midnight. I'm trying to develop a dynamic script that would have random storms occur and change cloud coverage randomly on some days and durring the day. If anyone else has any suggestions or example code or ways of optimizing what i did please comment.
     
    Last edited: Sep 26, 2011
  3. GeoAI 3

    GeoAI 3

    Joined:
    Nov 28, 2012
    Posts:
    1
    hi my friend this works really well. i have a question, how can i make it start from the day
     
  4. Sylux102

    Sylux102

    Joined:
    Mar 17, 2013
    Posts:
    15
    This is really old, but I have a more efficient way of doing this (still don't know how to make it start at a given time). Here's what you need to add to allow the time to continue to cycle. The only downside is that for one frame the time is the same.

    After the line in the Update function in the code in the OP, add an if statement like this:

    if(uniSky.GetTime() > 24)
    {
    uniSky.SetTime(0);
    }

    And it will cycle through a day, instead of stopping at night.