Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help fixing up my sun

Discussion in 'Scripting' started by Yourking77, Oct 29, 2016.

  1. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I need my sun to be able to change intensity and color at night right now I have enough code to change color but teh values are way off and I need help making it look right and also scrambling the colors a bit each day to make every day unique, I also need a way of making the sun speed up and slow down during winter and summer and a way to have moon phases. I need help deciding what is the best way to go about all this

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SunRotation : MonoBehaviour {
    6.  
    7.     //Variables that define the sun
    8.     [HideInInspector]
    9.     public GameObject sun;
    10.     [HideInInspector]
    11.     public Light sunLight;
    12.  
    13.     [Range(-180, 180)]
    14.     public float sunRotationOffset = -90;
    15.  
    16.     //Variables that track the suns gradient colors throughout the day
    17.     [HideInInspector]
    18.     public Gradient sunGradient;
    19.  
    20.     public Color night;
    21.     public Color earlyMorning;
    22.     public Color earlyNoon;
    23.     public Color noon;
    24.     public Color afterNoon;
    25.     public Color evening;
    26.  
    27.     //Variables that keep track of the time
    28.     [Range(0,24)]
    29.     public float timeOfDay = 12;
    30.  
    31.     public float secondsPerMinute = 60;
    32.     [HideInInspector]
    33.     public float secondsPerHour;
    34.     [HideInInspector]
    35.     public float secondsPerDay;
    36.  
    37.     [HideInInspector]
    38.     public float timeMultiplier = 1;
    39.  
    40.     void Start()
    41.     {
    42.         //Set up variables for the rest of the script
    43.         sun = gameObject;
    44.         sunLight = gameObject.GetComponent<Light>();
    45.  
    46.         SetupGradient();
    47.  
    48.         secondsPerHour = secondsPerMinute * 60;
    49.         secondsPerDay = secondsPerHour * 24;
    50.     }
    51.  
    52.     void Update()
    53.     {
    54.         timeOfDay += (Time.deltaTime / secondsPerDay) * timeMultiplier;
    55.  
    56.         if (timeOfDay >= 24) {
    57.             timeOfDay = 0;
    58.         }
    59.  
    60.         SunUpdate();
    61.     }
    62.  
    63.     void SetupGradient()
    64.     {
    65.         GradientColorKey[] colorKey = new GradientColorKey[7];
    66.         GradientAlphaKey[] alphaKey = new GradientAlphaKey[7];
    67.  
    68.         // the times are normalized
    69.         // 0 is the start, 1.0 is the end
    70.         colorKey[0].color = night;
    71.         colorKey[0].time = 0f;
    72.         colorKey[1].color = earlyMorning;
    73.         colorKey[1].time =  0.05f;
    74.         colorKey[2].color = earlyNoon;
    75.         colorKey[2].time = 0.1f;
    76.         colorKey[3].color = noon;
    77.         colorKey[3].time = 0.3f;
    78.         colorKey[4].color = afterNoon;
    79.         colorKey[4].time = 0.6f;
    80.         colorKey[5].color = evening;
    81.         colorKey[5].time = 0.8f;
    82.         colorKey[6].color = night;
    83.         colorKey[6].time = 0.9f;
    84.  
    85.         for (int i = 0; i < alphaKey.Length; ++i)
    86.             alphaKey[i].time = 1.0f;
    87.  
    88.         sunGradient.SetKeys(colorKey, alphaKey);
    89.     }
    90.  
    91.     public void SunUpdate()
    92.     {
    93.         sun.transform.localRotation = Quaternion.Euler(((timeOfDay / 24) * 360f) + sunRotationOffset, 90, 0);
    94.  
    95.         sunLight.color = sunGradient.Evaluate(sun.transform.localRotation.x);
    96.     }
    97. }
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    for intensity, color, sun speed (did you mean daytime or something?) you can use an animation curve, that way you can control it in the editor, and don't need to hardcode it
    the moon phases should be easy, IF you have just an image or model as the moon and you can change it during runtime
     
  3. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I meant I want it to change color throughout the day and for different seasons have different day lengths and such, sorry if I was not clear enough about that. I also want the sun to be able to change color during rainstorms and stuff like that also.

    I do not want to animate my sun, I assume an animation curve requires animation, though I could be wrong there are a lot of graphs and stuff in a lot of unities features and I am sure I could code something into my sun like that, that would be a neat idea.

    for the moon I would say create an array or system of arrays and just fill them with images for the moon, my first idea was to have an sphere and another invisible sphere to sort of circle it to block parts out but that would not work too well.