2D lighting: torch flicker and day-night cycle (Unity 6 URP / C#)

Two small scripts that drive URP Light 2D components from code. They go with the wiki page unity-2d-lighting, which explains the 2D Renderer, light types, blend styles, normal maps and shadows.

ScriptWhat it demonstratesWiki page
TorchFlicker2D.csSmooth flicker by sampling Mathf.PerlinNoise each frame and writing it to Light2D.intensity and Light2D.pointLightOuterRadius; a random noise offset per torch so a row of torches does not flicker in stepunity-2d-lighting, lighting-for-mood-and-guidance
DayNightCycle2D.csA looping time of day that sets a Global Light 2D’s color (from a Gradient) and intensity (from an AnimationCurve), and enables a list of night lights below a brightness thresholdunity-2d-lighting, lighting-for-mood-and-guidance

Both scripts need using UnityEngine.Rendering.Universal; because Light2D is part of the Universal Render Pipeline package, not the core engine. They were compile-checked against the URP 2D runtime assembly shipped with Unity 6000.3.11f1 (URP 17.3); they have not been play-tested in a scene by the wiki.

Unity requires one MonoBehaviour per file with a matching file name, so copy the .cs files into your project’s Assets/Scripts/ folder unchanged.

Scene setup (about 10 minutes)

  1. Start from the Universal 2D project template (it already uses the 2D Renderer), or check that your URP Asset’s renderer is a 2D Renderer.
  2. Add some sprites or a Tilemap. Leave their material on the default Sprite-Lit-Default; sprites on Sprite-Unlit-Default ignore 2D lights.
  3. Global light: the Universal 2D template’s SampleScene already contains a Global Light 2D. If not, add one with GameObject > Light > Global Light 2D.
  4. Day-night: create an empty GameObject named Lighting and add DayNightCycle2D. Adding it in the Editor fills in a starter colour gradient and intensity curve and finds the Global Light 2D for you; check the Global Light field is filled. Set Seconds Per Day to 30 while testing.
  5. Torch: add GameObject > Light > Spot Light 2D, place it on a wall sprite, set Color to a warm orange, set the outer value of Radius to about 4, and add TorchFlicker2D. Set Base Outer Radius to the same value as the light’s outer radius.
  6. Drag the torch light into the Night Lights array on DayNightCycle2D.
  7. Press Play. The scene should cycle from blue night to orange dawn to white noon and back, and the torch should switch on as it gets dark and flicker gently.

What to try

  • Duplicate the torch several times. They should flicker independently. Then set noiseOffset to 0 in Awake and watch them flicker in lock-step.
  • Raise Flicker Speed to 15. At what point does the flame stop reading as fire and start reading as a faulty electric light?
  • Replace Mathf.PerlinNoise with Random.Range(-1f, 1f) and compare the result: per-frame random values strobe rather than waver.
  • Turn on Shadows on the torch light and add a Shadow Caster 2D to a crate sprite in front of the wall.
  • For pixel art behind a Pixel Perfect Camera, compare the torch with Grid Snapping set to Upscale Render Texture and to Pixel Snapping.

Source files

2 items under this folder.