
Unity Particle System Culling Bug: Particles Disappearing Prematurely
February 17, 2026
Unity Reflection Probe Flickering Bug: Causes and Reliable Fixes
February 18, 2026A 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.






![Lighting in Unity: A Practical Guide for Game Developers There’s something satisfying about watching a flat, boring plane turn into a mountain range. That’s exactly what a height map does in Unity. With a simple grayscale image, you can shape landscapes, add depth to materials, and create worlds that feel real instead of flat. It’s one of those tools that looks technical at first, but once you understand it, it becomes surprisingly simple and powerful. In this guide, we’ll break down what a height map is, how it works in Unity, how to use it for terrain, how it differs from normal maps, and how to control it with C#. What Is a Height Map? A height map is a grayscale image where each pixel represents elevation. Black = lowest height White = highest height Gray = values in between Think of it like a topographic map, but simplified into brightness levels. Unity reads this image and uses the brightness values to push parts of a surface up or down. The result? Hills, valleys, cliffs, and surface details created from a simple image. Height Maps in Unity Terrain The most common use of height maps in Unity is terrain generation. Unity’s Terrain system allows you to import a height map and automatically generate a 3D landscape from it. How to Import a Height Map into Terrain Create a Terrain: GameObject → 3D Object → Terrain Select the Terrain object. Open the Terrain Inspector. Choose Import Raw under the heightmap settings. Select your grayscale RAW file. Once imported, Unity converts the grayscale values into elevation data. If your height map is smooth, you’ll get rolling hills. If it has sharp contrast, you’ll get steep cliffs. Height Map Resolution Matters Resolution affects how detailed your terrain will be. A low-resolution height map creates blocky terrain. A high-resolution height map creates smoother, more detailed landscapes. However, higher resolution also increases memory usage and processing cost. If you're building for mobile, balance detail with performance. Height Maps vs Normal Maps This is where many beginners get confused. Height Map Actually changes geometry (in terrain or displacement). Creates real depth. More performance cost if geometry changes. Normal Map Does NOT change geometry. Fakes lighting to simulate bumps. Much cheaper performance-wise. If you need real terrain shape, use a height map. If you just want surface detail like cracks or scratches, a normal map is usually better. Using Height Maps in Materials (Parallax & Displacement) Height maps are not limited to terrain. You can also use them in materials. In Unity’s Standard Shader (or URP/HDRP equivalents), height maps can be used for: Parallax Mapping – creates depth illusion without changing geometry. Displacement Mapping – actually modifies mesh vertices (HDRP). For example, if you apply a brick texture, adding a height map can make the mortar appear recessed and bricks raised. Creating Height Maps You can create height maps using: Photoshop or GIMP (grayscale images) Blender (baked displacement maps) World Machine or Gaea (terrain generation tools) Procedural generation with code The key is keeping it grayscale and avoiding compression artifacts. Generating a Height Map with Code You can also generate terrain procedurally using Perlin Noise. This is common in open-world or survival games. Here’s a simple example: [csharp] using UnityEngine; public class TerrainGenerator : MonoBehaviour { public Terrain terrain; public int depth = 20; public int width = 256; public int height = 256; public float scale = 20f; void Start() { terrain.terrainData = GenerateTerrain(terrain.terrainData); } TerrainData GenerateTerrain(TerrainData terrainData) { terrainData.heightmapResolution = width + 1; terrainData.size = new Vector3(width, depth, height); terrainData.SetHeights(0, 0, GenerateHeights()); return terrainData; } float[,] GenerateHeights() { float[,] heights = new float[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { heights[x, y] = Mathf.PerlinNoise(x / scale, y / scale); } } return heights; } } [/csharp] This script generates a terrain using Perlin Noise, which creates natural-looking hills and variation. Controlling Height Strength Sometimes height maps look too extreme. Other times they look flat. In terrain settings, you can adjust: Terrain height (Y scale) Brush strength (when sculpting manually) In materials, you can adjust height intensity inside the shader settings. Small adjustments make a big difference. Subtle depth often looks more realistic than exaggerated displacement. Common Problems and Fixes Terrain Looks Blocky Increase heightmap resolution or smooth the terrain. Edges Look Stretched Make sure your height map is square and uses proper dimensions (like 512x512 or 1024x1024). Lighting Looks Strange Check your normal settings and ensure lighting is baked or set correctly. When to Use Height Maps Use height maps when: You need large landscapes. You want realistic terrain shaping. You’re building procedural worlds. You need true geometric depth. Skip them when: You only need small surface detail. Performance is extremely limited. Final Thoughts Height maps are one of those tools that feel technical at first, but once you use them, they become creative tools. You’re not just editing numbers. You’re sculpting mountains. Carving valleys. Designing the shape of a world. Start simple. Import a grayscale image. Adjust the scale. Play with noise. Watch how small changes affect the landscape. Once you understand height maps, Unity stops feeling like a flat engine. It starts feeling like a world builder.](https://unityqueen.com/wp-content/uploads/2026/02/Lighting-in-Unity-150x150.jpg)



