We create a lightning jolt each time the lightning flashes. The jolt’s purpose is to calculate its Line2D
points, look pretty, then destroy itself.
Open up the lighting jolt scene, and let’s get to work.
Let’s take a look at the LightningJolt node’s settings. The further away from the source, the less energy lightning has. Is this true in real life? I have no idea! But thinning the line as it gets further away makes the shape look more exciting and focuses our attention on the point of impact.
We start by setting the width to 6
pixels and creating a new width curve. You can make the default color property whatever you like. When I think of lightning or electricity, I think of a light blue.
The capping setting further dictate the shape of the Line2D.
Set the Capping -> Joint Mode to Sharp to get jagged corners at the end of segments. We want round caps at the start and the end of the curve though. To do so, set both Capping -> Begin Cap Mode and Capping -> End Cap Mode to Round. They won’t be very noticeable, but it looks better than having rectangular start and end.
Set the Visibility -> Z Index to -1
so the jolt appears behind visuals with a higher value.
If an object has a Modulate (or color) that exceeds the base values, it’ll glow with as long as there’s a WorldEnvironment node present in the same scene. To make our jolt glow, toggle Raw and set the values above 1.0
. I used 1.3
in the red, green, and blue channels.
The Sparks node is our Particles2D
. Their position will be where the LightningJolt ends. Let’s take a look at the first set of settings.
We’ll emit the particles using the AnimationPlayer
so, for now, we’ll set Emitting to false
.
Let’s use a small Amount to keep the sparks subtle. I set it to 4
. With multiple flashes, there will already be plenty of particles flying around.
We don’t want the particles to repeat, so we set Time -> One Shot to true
. The explosiveness controls the period in which the particles are emitted. The closer to 1
, the closer in time the particles appear. We set this to 1
so all sparks fly together when the node starts emitting. Also, set Time -> Randomness to 1
to randomize the sparks’ lifetime.
Here are my settings so far.
Add a new ParticlesMaterial in Process Material -> Material and expand it. This is where we design the particles’ motion. First, ensure that Flags -> Disable Z is checked. We’re working in 2D and don’t want the particles using this third dimension.
Set the Direction -> Direction to (0, 0, 0)
and Direction -> Spread to 180
. This will allow our particles to shoot in any direction.
Set the Gravity -> Gravity to (0, 0, 0)
too because in this example, the sparks are in space and we don’t want our sparks to fall as if we were on Earth.
Let’s talk velocity! You can play around with these values, but I want the particles to have a decent speed and spin. I set the Velocity to 128
with a randomness of 0.5
. I want some variance in the velocity, but I still want them to have a defined shape when they’re emitted.
I set the angular velocity to 128
too, with 1
for the randomness.
Damping is one of my favorite settings. It adds friction to the particles’ movement. You’ll notice the higher the damping, the quicker the particles slow down. Play around with the value until the effect feels satisfying to you.
Add a scale curve so the particles appear to dissipate. In the scale property, create a curve and edit it so it tends towards 0
.
The final property in the particles material is the color. I set it to the same as the Line2D color above.
Let’s add a subtle glow to the sparks by pushing the RAW values of the color above 1.0
, as we did above.
If you set the particles to emit, you should see this result. I’ve also set the particles one_shot to false to loop the effect. Be sure to change these settings back once you’ve had a look at the result. We’ll be using the AnimationPlayer to control when the particles appear.
Let’s use an animation to sequence our sparks’ emission and destroy the jolt instance after fading it out. Select the AnimationPlayer, create a new animation called “destroy”, and toggle Autoplay on Load.
Add a track to start emitting
the Sparks as soon as the animation plays.
Also, have the self_modulate
fade out after a delay. To destroy the jolt, create a new function call track by clicking Add Track. Create a keyframe on that track at the two-second mark and call queue_free()
. Two seconds is enough time for our particles to finish their animation.
The AnimationPlayer documentation offers more information as to how to set these tracks up if you need it.
Next, we’ll dive into the lightning beam script and have it create multiple jolt effects.