Create a chain lightning effect?

I am trying to create a chain lightning ability that will jump from the player sprite to the three nearest enemies in order. I have a lightning sprite that is oriented to the right (0 degrees). My thought it to use tweening behavior to stretch it between the player object and the nearest enemies - is that the best approach for this?

What formula and event do I use to get the distance between the player and nearest enemies? My thought is to stretch the lightning sprite to the appropriate distance for each jump.

Any advice is appreciated. Thank you, Morgan.

Here’s a method I use in one of my many current projects. In a nutshell, it draws a series of small square sprites in a haphazard line from source to enemy. A brief explanation to how it works:

  • Sparky is a lightning emitting tower, that sends out a line of ‘sparks’ in short bursts. It fires the lightning at a set interval.
  • The divide by 6 in the first action effectively spaces the sprites out by 6 pixels. This also gives the number of particles to draw.
  • The x and y positions are determined by the lerp command. The _lerp is a counter to keep track of how many particles have been placed. This is also used to calculate the distance between source and target to place the particle. The random(4)-2 is to give it a bit of a jiggle effect (and yes, it could also be written as RandomInRange(-2, 2)).
  • The id is used so I can delete the whole line with one small event. The draw and erase happens every frame for a set amount of time.

The delete, done first:


Then the drawing of the lightning:

2 Likes

Thank you. That’s an approach I hadn’t thought of. I would really like to stretch the sprite I have, if possible. Currently I’m trying to use the sqrt(Object1.SqDistance(Object2)) formula to change the width of my sprite but it isn’t working.

Using a sprite is fine if it’s horizontal or vertical. But you’ll find correctly positioning a stretched and rotated sprite is incredibly difficult.

If it’s only horizontal, why don’t you just set the width to Object1.Distance(Object2)? Though this assumes Object1 and Object2 are the same height.

1 Like

I came up with a good workaround when messing around with your first suggestion. I created a shorter lightning sprite. I was going to have it reapawn from the tip until a collision was detected. It wasn’t working out how I wanted as the instances got confusing when there was more than one on the screen. Then I tried spawning the short lightning (with the origin and center point on the left tip), aiming it at a random enemy then changing the width until it collides with an enemy and is destroyed. It works! Quite beautifully, actually. Thank you for the inspiration!

1 Like