Electricity in Unity — Part 2 of 3
Part 1 of this series covered some vector math basics, and the creation of a single, long electrical arc. Now we get into creating a Jacob’s Ladder effect.
Jacob’s Ladder Scene
The Jacob’s Ladder Scene is a sphere in the ground with two rods at an angle of 15° off of the vertical around the X axis, creating a 30° internal angle:
An empty GameObject called PivotPoint is down at the intersection of the rods. This is used for tracing the joints of the arc. Each rod has a pair of empty GameObjects called SparkOrigin and SparkEnd, shown in green and red respectively:
The spark location starts and ends at these points.
Spark Movement
A direction vector is created for each rod by subtracting sparkOrigin from sparkEnd:
A point is created from each sparkOrigin, and it is moved along the appropriate direction vector by an appropriate speed for each frame. When the points are close enough to the sparkEnd, the arc has completed its run.
The Arc
This is created from several points generated at each frame. The points are generated by creating a vector from the current point on one rod, and the pivot point. This vector is then rotated toward the other rod in 5° increments:
At each increment, an new point is created by adding the pivot point to the vector, resulting in points along the arc:
These points are perturbed for each frame so the Jacob’s Ladder arc is animated.
The Code
Illumination
I added a spot light to the scene so the arc would illuminate the wall behind it. SetActive is used to turn it on and off with the arc. To move the light with the arc, I selected one of the arc points near the middle (there is no middle one), and set the light’s position to that point. On first run, there was an annoying flicker. That was caused by the arc’s line renderer, so I nudged the light back behind the arc a little to prevent the line from interfering with the light:
The results are more dramatic than the arc seen in part 1 of this series:
I thought about just moving the light independently down the middle between the rods, but this method worked well enough.
The Jacob’s Ladder effect is simple to implement, and the results are pretty effective.
In the final part of this series, we get into detail on lightning generation…
If you missed part one…