This is the Unity version. See the matching Unreal Engine Niagara tutorial.
Back to the elemental comparison and recipes
What you will build
Create an angular bolt joining five points and display it for about 0.08 seconds. Core is the thin white center; Glow is a wider, softer line behind it. Each uses Line Renderer, a component that draws a line between points. First make the shape, then use a short C# script to show and hide both lines together.
Preparation: make a small test scene
Use Unity 2022.3 LTS with URP 14 and save a separate practice Scene. A Scene is the workspace containing the floor, camera and effect. Hierarchy lists its objects; Inspector shows settings for the selected object. If you use another Unity version, including Unity 6, menu names and positions may differ.
- Right-click an empty area in Hierarchy, choose Create Empty and name it EF_Lightning. In Inspector, open the Transform menu and choose Reset to restore its position, rotation and scale. The examples treat one Unity unit as one meter and keep parent scale at (1,1,1).
Keep the practice scene and its assets together - Place a unit cube at (2,0.5,0) as a size reference, away from the effect origin. Add a floor cube at (0,-0.05,0), scale (4,0.1,4), with its top at Y=0.
Scale reference - Frame the origin from an elevated angle in Scene view. Select Main Camera and use GameObject → Align With View, then verify the floor/work area in Game view.
Match Main Camera to the Scene view - Inspect the Game view in Play mode against mid-gray, initially without bloom or strong grading. Keep a Directional Light for Lit materials, whose shading responds to lights. Bloom is the glow around bright areas; leave it off while checking shapes.
Enter Play and inspect shape and motion in Game view
Build Core, then Glow, then the script that switches them on and off. Check each part on its own before adding the next part.
Object relationships
EF_Lightning groups the whole effect. In the list below, each indented object is a child of the object above it. Start with the root and add the children in the order explained below.
EF_Lightning (TutorialLightningPulse)
Core (Line Renderer)
Glow (Line Renderer)
Check width and color inputs first
Width controls line thickness. Open its Curve and keep the start and end keys at the same height for an even line. Color and Alpha are separate: Alpha 0 is invisible and Alpha 1 applies no transparency reduction.
A script is a C# file that tells an object how to behave. Save the .cs file inside Assets using the exact filename shown below. Check Console for red errors, then drag the script onto the specified object. Keep it outside folders named Editor.
1. Prepare a grayscale mask
Mask_White is a mask: white marks the visible shape, black marks hidden areas and gray creates partial transparency. Read R, the red component of this grayscale image, into the material’s opacity. The material adds color later. Finish the material setup below so the black area becomes transparent.
- In Photoshop create a 512×512 RGB, 8-bit image with a black background and a separate layer for the white shape.
RGB / 8 bit - Fill the entire shape layer white. This first example uses R=1 everywhere; Line Renderer supplies the shape.
Line Renderer supplies the bolt shape - Leave out noise or patterns until the line renders correctly.
Connect image brightness to opacity: black disappears - Export Mask_White.png and drag it into Unity Project. Start with Default type, sRGB off, Bilinear filtering and Clamp wrapping; select the imported PNG, click Apply in Inspector, and check that the preview contains the white shape and black margin.
Connect image brightness to opacity: black disappears
512 is the image size used for this exercise. Keep the editable PSD. Compare a smaller resolution at the effect’s actual screen size before choosing the final size. Check PNG file size separately from memory used by the imported Unity texture.
2. Build the mask-and-color material
A Material holds appearance settings such as the image, color and transparency. Shader Graph builds the calculations by connecting nodes. Here the grayscale image supplies the shape and Tint supplies the color. Unlit displays that color without directional-light shading.
- Right-click in Project and choose Create → Shader Graph → URP → Unlit Shader Graph. Name it SG_Bolt and double-click to open it. Set Transparent surface, Additive blending and Alpha Clipping off.
RGB / 8 bit - In the Blackboard, use + to add Texture2D MaskTex, Color Tint and Float Intensity and Opacity. Enable Exposed for each so it appears in the Material Inspector. MaskTex chooses the image, Tint chooses its color, Intensity multiplies brightness and Opacity multiplies transparency. Set the default Tint to white with alpha one and both floats to one; verify them on the material too.
Line Renderer supplies the bolt shape - Right-click empty graph space to add nodes, the small calculation blocks. Connect MaskTex to the Texture input of Sample Texture 2D. Use Multiply nodes to multiply sample R, Vertex Color A and Opacity, then pass the result through Saturate into Fragment Alpha. Multiply performs multiplication; Saturate limits the result to 0–1. Feed Tint RGB × Vertex Color RGB × Intensity into Base Color.
Connect image brightness to opacity: black disappears - Create M_Bolt from the saved graph, assign its PNG to MaskTex and set Tint to white. Save the Material here. Assign it to the Core and Glow Line Renderers after creating that object in the later steps. Renderer is the component that draws the object on screen.
Connect image brightness to opacity: black disappears
Vertex Color passes the particle or line color and Alpha into the material. Multiplying its A makes settings such as Color over Lifetime visibly fade the result. For Particle System, open Renderer and check that Custom Vertex Streams includes Color and supplies the expected UV coordinates, which choose where the image is sampled.
See the Shader Graph walkthrough and mask workflow for node and export details.
3. Create the Core and Glow lines
- Create Core under EF_Lightning, reset its local Transform, and add Line Renderer. Disable Use World Space and Loop and use View alignment.
Join the same five points; show both for about 0.08 s - Set Positions size to five and enter the local coordinates below. Use a constant width of 0.025 and assign M_Bolt.
Join the same five points; show both for about 0.08 s - Duplicate Core as Glow with identical positions, width 0.08 and a separate pale-blue material with Opacity 0.3.
Join the same five points; show both for about 0.08 s - Keep both line-color endpoints white with alpha one, since the graph reads Vertex Color. Verify the thin core is readable.
Join the same five points; show both for about 0.08 s
| Setting | Value | Purpose / check |
|---|---|---|
| Positions / Element 0 | (0, 0, 0) | Lower endpoint. |
| Positions / Element 1 | (0.18, 0.5, 0) | First bend. |
| Positions / Element 2 | (-0.12, 1, 0) | Bend the opposite way. |
| Positions / Element 3 | (0.12, 1.5, 0) | Next change of direction. |
| Positions / Element 4 | (0, 2, 0) | Upper endpoint. |
Give Core and Glow the same point coordinates. Core is a thin white line; Glow is a wider, pale-blue line in the same position. Check Core alone first, then add Glow. Add bloom, the glow around bright areas, after the angular shape is readable.
4. Preview a short pulse with a script
Create TutorialLightningPulse.cs, replace its contents below and match the file/class name. After compilation attach it to EF_Lightning. The Inspector command avoids changing the project input system.
You can also download TutorialLightningPulse.cs. It matches the listing. Keep the .cs extension and place it under Unity Assets.
using System.Collections;
using UnityEngine;
public sealed class TutorialLightningPulse : MonoBehaviour
{
[SerializeField] private LineRenderer[] lines = new LineRenderer[0];
[SerializeField, Min(0.01f)] private float duration = 0.08f;
private Coroutine pulse;
private void OnEnable() => SetVisible(false);
[ContextMenu("Play Bolt")]
public void PlayBolt()
{
if (!Application.isPlaying) return;
if (pulse != null) StopCoroutine(pulse);
pulse = StartCoroutine(ShowPulse());
}
private IEnumerator ShowPulse()
{
SetVisible(true);
yield return new WaitForSeconds(duration);
SetVisible(false);
pulse = null;
}
private void OnDisable()
{
if (pulse != null) StopCoroutine(pulse);
pulse = null;
SetVisible(false);
}
private void SetVisible(bool value)
{
if (lines == null) return;
foreach (LineRenderer line in lines)
if (line != null) line.enabled = value;
}
}
- Set Lines size to two and drag in Core and Glow Line Renderers; set Duration to 0.08.
Join the same five points; show both for about 0.08 s - Enter Play: the lines hide. Select Play Bolt from the component header menu and verify a single brief appearance.
Join the same five points; show both for about 0.08 s - For easier shape inspection temporarily use duration 0.3, then return to 0.08.
Join the same five points; show both for about 0.08 s
WaitForSeconds follows timeScale. A pause-independent effect needs an appropriate realtime design. This controls presentation only; gameplay damage is separate.
With Use World Space off, each point is relative to the object holding that Line Renderer. Keep Core and Glow at local position zero, rotation zero and scale one. The same five point coordinates then draw two overlapping lines.
5. Add branches and glow after the main bolt
Create a three-point branch as another Line Renderer, starting at an existing bolt point. Make it narrower and add it to the Lines array so it follows the same pulse.
Add bloom only if needed. Increasing shader intensity does not automatically light neighboring objects.
Check pulse-time and width units
A line width of 0.025 is in world units, not pixels. Camera distance/FOV changes screen thickness; inspect width and framing before raising intensity.
WaitForSeconds resumes on a frame after the elapsed duration, so the display is not guaranteed to be exactly 0.08 seconds. It also waits while timeScale is zero.
From the first playback to saving
Temporarily use Duration 0.3 and run Play Bolt. Verify coincident Core/Glow lines and that both disappear, then restore 0.08.
Entering Play alone does not fire the bolt. Check Lines and invoke Play Bolt once. If the pulse is hard to see, inspect at duration 0.3, restore 0.08 and verify both Core and Glow switch off.
Inspector values tested in Play may reset when you stop. Write down the values you want to keep, exit Play and enter them again, then save the Scene. Drag the completed root object into Project to save a Prefab, a reusable file containing the whole effect.
Troubleshoot one cause at a time
| Symptom | First check | Comparison |
|---|---|---|
| Always hidden in Play | Array references and command | Assign both lines and invoke Play Bolt while playing. |
| Line detaches during movement | Use World Space | Disable it on both; match each local Transform and all Positions. |
| Only a broad glow is visible | Glow width and intensity | Hide Glow and restore it after checking Core. |
Completion checks: compare under matching conditions
- Read the angular thin core without bloom.
Join the same five points; show both for about 0.08 s - Invoke once and verify both lines disappear afterward.
Join the same five points; show both for about 0.08 s - Move/rotate the root and verify both lines follow together.
Join the same five points; show both for about 0.08 s - For repeated discharges preserve readable gaps rather than toggling every frame.
Join the same five points; show both for about 0.08 s
Continue by comparing another element
Return to the eight-element comparison to compare shape, motion and persistence. Another exercise is the wind recipe.