قالب وردپرس قالب وردپرس قالب فروشگاهی وردپرس وردپرس آموزش وردپرس

Unity Dynamic Fog and Ambient Light Curves for Day and Night Cycles

Unity Particle System Culling Bug: Particles Disappearing Prematurely
February 17, 2026
Unity Reflection Probe Flickering Bug
Unity Reflection Probe Flickering Bug: Causes and Reliable Fixes
February 18, 2026

Unity Dynamic Fog and Ambient Light Curves for Day and Night Cycles

Unity Dynamic Fog and Ambient Light Curves for Day and Night Cycles

A simple sun rotation is not enough to make a day-night cycle feel realistic. The real atmosphere comes from smooth transitions in fog density, fog color, and ambient light intensity.

Instead of hardcoding values with if statements, the professional way to control these transitions is by using Animation Curves. Curves allow you to design how lighting and fog behave visually across the entire day.

This guide shows how to implement dynamic fog and ambient light curves properly.

Why Use Animation Curves?

Animation Curves give you full control over:

  • Ambient light intensity throughout the day
  • Fog density increasing at night
  • Fog color shifting at sunset
  • Smooth sunrise and sunset transitions

Instead of fixed logic, you design lighting visually in the Inspector.

Step 1: Scene Preparation

  • Have a Directional Light (Sun)
  • Enable Fog in Lighting Settings
  • Set RenderSettings → Fog Mode (Linear or Exponential)

Make sure fog is enabled before running the script.

Step 2: Create the Dynamic Environment Script

Create a new script called DynamicEnvironmentController.cs.

using UnityEngine;

public class DynamicEnvironmentController : MonoBehaviour
{
    [Header("Time Settings")]
    public float dayDuration = 120f;
    private float timeOfDay = 0f;

    [Header("Sun")]
    public Light sun;

    [Header("Ambient Light Curve")]
    public AnimationCurve ambientIntensityCurve;

    [Header("Fog Settings")]
    public AnimationCurve fogDensityCurve;
    public Gradient fogColorGradient;

    void Update()
    {
        UpdateTime();
        UpdateSun();
        UpdateAmbientLight();
        UpdateFog();
    }

    void UpdateTime()
    {
        timeOfDay += Time.deltaTime / dayDuration;
        if (timeOfDay > 1f)
            timeOfDay = 0f;
    }

    void UpdateSun()
    {
        if (sun != null)
        {
            sun.transform.localRotation =
                Quaternion.Euler((timeOfDay * 360f) - 90f, 170f, 0f);
        }
    }

    void UpdateAmbientLight()
    {
        float intensity = ambientIntensityCurve.Evaluate(timeOfDay);
        RenderSettings.ambientIntensity = intensity;
    }

    void UpdateFog()
    {
        RenderSettings.fogDensity = fogDensityCurve.Evaluate(timeOfDay);
        RenderSettings.fogColor = fogColorGradient.Evaluate(timeOfDay);
    }
}

Step 3: Configure Animation Curves

After attaching the script to an empty GameObject:

  • Create a curve for Ambient Intensity
  • Create a curve for Fog Density
  • Adjust keys directly in the Inspector

Example curve design:

  • 0.0 → Night (low ambient, higher fog)
  • 0.25 → Sunrise
  • 0.5 → Midday (high ambient, low fog)
  • 0.75 → Sunset
  • 1.0 → Night again

You control the shape visually instead of writing complex code.

Step 4: Configure Fog Color Gradient

The Gradient field lets you blend fog color smoothly.

Example gradient design:

  • Dark blue at 0.0 (midnight)
  • Orange-pink at 0.25 (sunrise)
  • Light blue at 0.5 (day)
  • Orange-red at 0.75 (sunset)
  • Dark blue at 1.0 (night)

This creates realistic atmospheric color transitions.

Recommended Fog Modes

  • Exponential: Smooth and natural for open worlds
  • Linear: Better control for stylized games

For most outdoor scenes, Exponential fog gives better results.

Performance Considerations

  • Updating curves every frame is lightweight
  • Avoid extremely high fog density on mobile
  • Keep ambient intensity within reasonable range (0.1 to 1.2)

These updates are inexpensive compared to shadows or post-processing.

Optional Enhancement: Smooth Sun Intensity Curve

You can also replace fixed sun intensity with a curve:

[Header("Sun Intensity")]
public AnimationCurve sunIntensityCurve;

void UpdateSun()
{
    if (sun != null)
    {
        sun.transform.localRotation =
            Quaternion.Euler((timeOfDay * 360f) - 90f, 170f, 0f);

        sun.intensity = sunIntensityCurve.Evaluate(timeOfDay);
    }
}

This gives full artistic control over brightness transitions.

Common Mistakes

  • Using hardcoded if statements instead of curves
  • Setting fog density too high at night (causes flat look)
  • Forgetting to enable Fog in Lighting Settings
  • Not testing transitions at accelerated time speed

Final Thoughts

Dynamic fog and ambient light curves transform a basic day-night cycle into a believable atmosphere system. Animation Curves allow smooth, artistic transitions without complicated logic.

Instead of reacting to time with rigid code, you design lighting visually and let Unity interpolate smoothly throughout the day.

Once implemented correctly, your world will feel alive — with soft mornings, bright midday light, warm sunsets, and deep atmospheric nights.

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar