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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How can you Disable/Enable global fog on your camera?? C#

Discussion in 'Scripting' started by Jeremie-de-Vos, Nov 17, 2016.

  1. Jeremie-de-Vos

    Jeremie-de-Vos

    Joined:
    Jun 5, 2015
    Posts:
    120
    Hey,
    So, i have done a quick search but can't find the solution.
    What i want to do is enable or disable the "GlobalFog" script on my camera.
    All Help is Appreciated!!



    Code (CSharp):
    1. using UnityEngine;
    2. using UnityStandardAssets.ImageEffects;
    3.  
    4. public class DayNightController : MonoBehaviour
    5. {
    6.  
    7.     public Light sun;
    8.     public float secondsInFullDay = 120f;
    9.  
    10.     [Range(0, 1)]
    11.     public float currentTimeOfDay = 0;
    12.  
    13.     [HideInInspector]
    14.     public float timeMultiplier = 1f;
    15.  
    16.  
    17.     GameObject PlayerCam;
    18.  
    19.     float sunInitialIntensity;
    20.  
    21.     void Start()
    22.     {
    23.         PlayerCam = GameObject.FindGameObjectWithTag("MainCamera");
    24.         sunInitialIntensity = sun.intensity;
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         UpdateSun();
    30.  
    31.         currentTimeOfDay += (Time.deltaTime / secondsInFullDay) * timeMultiplier;
    32.  
    33.         if (currentTimeOfDay >= 1)
    34.         {
    35.             currentTimeOfDay = 0;
    36.         }
    37.  
    38.         //Change fog
    39.         if (currentTimeOfDay <= 0.2)
    40.         {
    41.             Debug.Log("Fog need to be of!!");
    42.         }
    43.  
    44.         if (currentTimeOfDay >= 0.2)
    45.         {
    46.             if (currentTimeOfDay <= 0.78)
    47.                 Debug.Log("Change Fog on");
    48.         }
    49.  
    50.         if (currentTimeOfDay >= 0.78)
    51.         {
    52.                 Debug.Log("Fog need to be of!!");
    53.         }
    54.     }
    55.  
    56.     void UpdateSun()
    57.     {
    58.         sun.transform.localRotation = Quaternion.Euler((currentTimeOfDay * 360f) - 90, 170, 0);
    59.  
    60.         float intensityMultiplier = 1;
    61.         if (currentTimeOfDay <= 0.23f || currentTimeOfDay >= 0.75f)
    62.         {
    63.             intensityMultiplier = 0;
    64.         }
    65.         else if (currentTimeOfDay <= 0.25f)
    66.         {
    67.             intensityMultiplier = Mathf.Clamp01((currentTimeOfDay - 0.23f) * (1 / 0.02f));
    68.         }
    69.         else if (currentTimeOfDay >= 0.73f)
    70.         {
    71.             intensityMultiplier = Mathf.Clamp01(1 - ((currentTimeOfDay - 0.73f) * (1 / 0.02f)));
    72.         }
    73.  
    74.         sun.intensity = sunInitialIntensity * intensityMultiplier;
    75.     }
    76. }
    77.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityStandardAssets.ImageEffects;
    3. public class DayNightController : MonoBehaviour
    4. {
    5.     public Light sun;
    6.     public float secondsInFullDay = 120f;
    7.     [Range(0, 1)]
    8.     public float currentTimeOfDay = 0;
    9.     [HideInInspector]
    10.     public float timeMultiplier = 1f;
    11.     GameObject PlayerCam;
    12.     float sunInitialIntensity;
    13.     void Start()
    14.     {
    15.         PlayerCam = GameObject.FindGameObjectWithTag("MainCamera");
    16.         sunInitialIntensity = sun.intensity;
    17.     }
    18.     void Update()
    19.     {
    20.         UpdateSun();
    21.         currentTimeOfDay += (Time.deltaTime / secondsInFullDay) * timeMultiplier;
    22.         if (currentTimeOfDay >= 1)
    23.         {
    24.             currentTimeOfDay = 0;
    25.         }
    26.         //Change fog
    27.         if (currentTimeOfDay <= 0.2)
    28.         {
    29.             RenderSettings.fog = false;
    30.         }
    31.         if (currentTimeOfDay >= 0.2)
    32.         {
    33.             if (currentTimeOfDay <= 0.78)
    34.                 RenderSettings.fog = true;;
    35.         }
    36.         if (currentTimeOfDay >= 0.78)
    37.         {
    38.             RenderSettings.fog = false;
    39.         }
    40.     }
    41.     void UpdateSun()
    42.     {
    43.         sun.transform.localRotation = Quaternion.Euler((currentTimeOfDay * 360f) - 90, 170, 0);
    44.         float intensityMultiplier = 1;
    45.         if (currentTimeOfDay <= 0.23f || currentTimeOfDay >= 0.75f)
    46.         {
    47.             intensityMultiplier = 0;
    48.         }
    49.         else if (currentTimeOfDay <= 0.25f)
    50.         {
    51.             intensityMultiplier = Mathf.Clamp01((currentTimeOfDay - 0.23f) * (1 / 0.02f));
    52.         }
    53.         else if (currentTimeOfDay >= 0.73f)
    54.         {
    55.             intensityMultiplier = Mathf.Clamp01(1 - ((currentTimeOfDay - 0.73f) * (1 / 0.02f)));
    56.         }
    57.         sun.intensity = sunInitialIntensity * intensityMultiplier;
    58.     }
    59. }
     
  4. tsatellite2000

    tsatellite2000

    Joined:
    Jan 10, 2019
    Posts:
    4
    RenderSettings.fog = false; //true
     
    StringAtlas likes this.
  5. Jeremie-de-Vos

    Jeremie-de-Vos

    Joined:
    Jun 5, 2015
    Posts:
    120
    uhm this was a post from 4 years ago :p
     
    SparrowGS likes this.
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Not to mention it was already answered..
     
    xVergilx likes this.
  7. tsatellite2000

    tsatellite2000

    Joined:
    Jan 10, 2019
    Posts:
    4
    Ok , it can be 12 years old post and few years later someone finds it. It doesn't matter as long as the code will work in the future. And no need to read entire script for a line of code
     
    Hellgeist likes this.
  8. jmtc1977

    jmtc1977

    Joined:
    Jul 15, 2018
    Posts:
    1
    it works on Unity 2019