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.
| Script | What it demonstrates | Wiki page |
|---|---|---|
TorchFlicker2D.cs | Smooth 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 step | unity-2d-lighting, lighting-for-mood-and-guidance |
DayNightCycle2D.cs | A 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 threshold | unity-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)
- 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.
- Add some sprites or a Tilemap. Leave their material on the default Sprite-Lit-Default; sprites on Sprite-Unlit-Default ignore 2D lights.
- Global light: the Universal 2D template’s
SampleScenealready contains a Global Light 2D. If not, add one with GameObject > Light > Global Light 2D. - Day-night: create an empty GameObject named
Lightingand addDayNightCycle2D. 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. - 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. - Drag the torch light into the Night Lights array on
DayNightCycle2D. - 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
noiseOffsetto0inAwakeand 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.PerlinNoisewithRandom.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.