# How does this sample lightning work?

**URL:** <https://discussions.unity.com/t/how-does-this-sample-lightning-work/601932>\
**Category:** Visual Scripting\
**Tags:** Scripting\
**Created:** [October 18, 2015, 11:56am UTC](https://discussions.unity.com/t/how-does-this-sample-lightning-work/601932 "2015-10-18T11:56:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nanako](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/nanako/32/497390_2.png) [@Nanako](https://discussions.unity.com/u/Nanako)\
**Post date:** [October 18, 2015, 11:56am UTC](https://discussions.unity.com/t/how-does-this-sample-lightning-work/601932/1 "2015-10-18T11:56:20Z")

</div>

Hi all, i’m currently poking around in a sample scene named Lightning Bolt, it’s from the unity built in assets somewhere.

I’m currently attempting to dissect how this works, and i’m not quite getting it.  
Here’s how it looks in practise:  
 ![](https://europe1.discourse-cdn.com/unity/original/4X/c/0/4/c0469d4935175183e791f1983996586707fa3c7b.gif)

It undulates and moves around. it’s not what i’d call a perfect lightning animation, but it’s a decent start.

Maybe relevant, here’s the inspector properties of the emitter, it has two particle related components

 ![](https://europe1.discourse-cdn.com/unity/original/4X/0/c/4/0c4fb6a8edf3fd8125c057a02cc1606622d1edb1.png)

As for how it works, there’s some part of it which is constantly moving, but i’m not clear on what, or why. The full code for the lightning emitter is: [Legobet88 = Daftar Situs Slot Online Terpercaya & Slot Gacor Resmi Hari Ini di Login Bet88](http://www.pasteall.org/61771)

As far as i can tell, all the magic happens on line 49, where the position of something is being set. if i comment that out, the effect looks like this:

![](https://europe1.discourse-cdn.com/unity/original/4X/c/9/f/c9fe4ff39ffc93259f2a025321c8e004c939a98a.png)

there’s no bolt or beam or anything now, it’s just particles floating around a sphere.

I’m completely stumped here, what is going on, how does this lightning work?

And as a side note, i’m interested in lightning in general, if anyone knows other/better methods of making it, please do let me know

---

<div class="post-metadata">

**Author:** ![Kurt-Dekker](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kurt-dekker/32/101578_2.png) [@Kurt-Dekker](https://discussions.unity.com/u/Kurt-Dekker)\
**Post date:** [October 18, 2015, 3:38pm UTC](https://discussions.unity.com/t/how-does-this-sample-lightning-work/601932/2 "2015-10-18T15:38:40Z")

</div>

You pretty much have it.

Analyzing a bit further, it looks like the script makes a Perlin noise object which gives you a source of random-ish noise, and then each frame it will iterate over all the particles, progressively setting their ongoing position, perturbed by the output of the Perlin noise object.

Where in the Perlin noise field they draw from is then slowly moved by the progress of the timex/timey/timez variables, which creep along over time. Since each offset is relative to the previous, the lightning tends to “whip” as it moves around.

I like to use fractal subdivisions for lightning, which lets you generate a crackling arc between any number of given fixed points, like between two electrodes. If I had to do it open-ended like this, I might still use two ends and cause the lightning generation to stop halfway to the destination node, which might also move back and forth to cause the “trailing” effect. There’s a ton of different ways to get lightning going, and that doesn’t even mention branching.
