Advertisement
  1. Code
  2. Coding Fundamentals
  3. Game Development

How to Generate Shockingly Good 2D Lightning Effects in Unity (JavaScript)

Scroll to top
Read Time: 42 min

There are plenty of uses for lightning effects in games, from background ambiance during a storm to the devastating lightning attacks of a sorcerer. In this tutorial, I'll explain how to programmatically generate awesome 2D lightning effects: bolts, branches, and even text.

This tutorial is written specifically for Unity, with all code snippets in JavaScript. The same tutorial is also available with C# code. If you don't use Unity, take a look at this platform-agnostic version of the same tutorial; it's written for XNA, but you should be able to use the same techniques and concepts in any gamedev engine and platform.

Demo

Check out the demo below:

Click the Unity object, then use the number keys to switch between demos. Some demos require you to click in one or two locations to activate them.

Basic Setup

To get started, you'll need to create a new 2D project in Unity. Name it whatever you'd like. In Unity, create four folders: Materials, Prefabs, Scripts, and Sprites.

Next, click the Main Camera and make sure that its Projection is set to Orthographic. Set the camera's Size to 10.

Right-click on the Materials folder and select Create > Material. Rename it to Additive. Select this material and change its Shader to Particles > Additive. This will help your lightning "pop" later on.

Step 1: Draw a Glowing Line

The basic building block we need to make lightning from is a line segment. Start by opening your favourite image editing software and drawing a straight line of lightning with a glow effect. Here's what mine looks like:

We want to draw lines of different lengths, so we will cut the line segment into three pieces as shown below (crop your image as necessary). This will allow us to stretch the middle segment to any length we like. Since we are going to be stretching the middle segment, we can save it as only a single pixel thick. Also, as the left and right pieces are mirror images of each other, we only need to save one of them; we can flip it in the code.

Drag your image files onto the Sprites folder in the Project panel. This will import the image files into the Unity project. Click on the sprites to view them in the Inspector panel. Make sure the Texture Type is set to Sprite(2D \ uGUI), and set the Packing Tag to Line.

The Packing Tag will help Unity save on draw calls when drawing our lightning, so make sure you give both sprites the same Packing Tag or else it won't improve performance.

Now, let's declare a new class to handle drawing line segments:

1
#pragma strict
2
3
class LineJS extends MonoBehaviour
4
{
5
    //Start

6
    public var A: Vector2;
7
    
8
    //End

9
    public var B: Vector2;
10
    
11
    //Thickness of line

12
    public var Thickness: float;
13
    
14
    //Children that contain the pieces that make up the line

15
    public var StartCapChild : GameObject;
16
    public var LineChild : GameObject;
17
    public var EndCapChild : GameObject;
18
    
19
    //Create a new line

20
    public function Line(a : Vector2, b : Vector2, thickness : float)
21
    {
22
        A = a;
23
        B = b;
24
        Thickness = thickness;
25
    }
26
    
27
    //Used to set the color of the line

28
    public function SetColor(color : Color)
29
    {
30
        StartCapChild.GetComponent(SpriteRenderer).color = color;
31
        LineChild.GetComponent(SpriteRenderer).color = color;
32
        EndCapChild.GetComponent(SpriteRenderer).color = color;
33
    }
34
    
35
    //...

36
}

A and B are the line's endpoints. By scaling and rotating the pieces of the line, we can draw a line of any thickness, length, and orientation.

Add the following Draw() method to the bottom of the LineJS class:

1
//Will actually draw the line

2
public function Draw()
3
{
4
    var difference : Vector2 = B - A;
5
    var rotation : float = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
6
    
7
    //Set the scale of the line to reflect length and thickness

8
    LineChild.transform.localScale = new Vector3(100 * (difference.magnitude / LineChild.GetComponent(SpriteRenderer).sprite.rect.width), 
9
                                                 Thickness, 
10
                                                 LineChild.transform.localScale.z);
11
    
12
    StartCapChild.transform.localScale = new Vector3(StartCapChild.transform.localScale.x, 
13
                                                     Thickness, 
14
                                                     StartCapChild.transform.localScale.z);
15
    
16
    EndCapChild.transform.localScale = new Vector3(EndCapChild.transform.localScale.x, 
17
                                                   Thickness, 
18
                                                   EndCapChild.transform.localScale.z);
19
    
20
    //Rotate the line so that it is facing the right direction

21
    LineChild.transform.rotation = Quaternion.Euler(new Vector3(0,0, rotation));
22
    StartCapChild.transform.rotation = Quaternion.Euler(new Vector3(0,0, rotation));
23
    EndCapChild.transform.rotation = Quaternion.Euler(new Vector3(0,0, rotation + 180));
24
    
25
    //Move the line to be centered on the starting point

26
    LineChild.transform.position = new Vector3 (A.x, A.y, LineChild.transform.position.z);
27
    StartCapChild.transform.position = new Vector3 (A.x, A.y, StartCapChild.transform.position.z);
28
    EndCapChild.transform.position = new Vector3 (A.x, A.y, EndCapChild.transform.position.z);
29
    
30
    //Need to convert rotation to radians at this point for Cos/Sin

31
    rotation *= Mathf.Deg2Rad;
32
    
33
    //Store these so we only have to access once

34
    var lineChildWorldAdjust : float = LineChild.transform.localScale.x * LineChild.GetComponent(SpriteRenderer).sprite.rect.width / 2f;
35
    var startCapChildWorldAdjust : float = StartCapChild.transform.localScale.x * StartCapChild.GetComponent(SpriteRenderer).sprite.rect.width / 2f;
36
    var endCapChildWorldAdjust : float = EndCapChild.transform.localScale.x * EndCapChild.GetComponent(SpriteRenderer).sprite.rect.width / 2f;
37
    
38
    //Adjust the middle segment to the appropriate position

39
    LineChild.transform.position += new Vector3 (.01f * Mathf.Cos(rotation) * lineChildWorldAdjust, 
40
                                                 .01f * Mathf.Sin(rotation) * lineChildWorldAdjust,
41
                                                 0);
42
    
43
    //Adjust the start cap to the appropriate position

44
    StartCapChild.transform.position -= new Vector3 (.01f * Mathf.Cos(rotation) * startCapChildWorldAdjust, 
45
                                                     .01f * Mathf.Sin(rotation) * startCapChildWorldAdjust,
46
                                                     0);
47
    
48
    //Adjust the end cap to the appropriate position

49
    EndCapChild.transform.position += new Vector3 (.01f * Mathf.Cos(rotation) * lineChildWorldAdjust * 2, 
50
                                                   .01f * Mathf.Sin(rotation) * lineChildWorldAdjust * 2,
51
                                                   0);
52
    EndCapChild.transform.position += new Vector3 (.01f * Mathf.Cos(rotation) * endCapChildWorldAdjust, 
53
                                                   .01f * Mathf.Sin(rotation) * endCapChildWorldAdjust,
54
                                                   0);
55
}

The way we position the middle segment and the caps will make them join seamlessly when we draw them. The start cap is positioned at point A, the middle segment is stretched to the desired width, and the end cap is rotated 180° and drawn at point B.

Now we need to create a prefab for our LineJS class to work with. In Unity, from the menu, select GameObject > Create Empty. The object will appear in your Hierarchy panel. Rename it to LineJS and drag your LineJS script onto it. It should look something like the image below.

We'll use this object as a container for the pieces of our line segment.

Now we need to create objects for the pieces of our line segment. Create three Sprites by selecting GameObject > Create Other > Sprite from the menu. Rename them toStartCap, MiddleSegment, and EndCap. Drag them onto our LineJS object so that they become its children—this should look something like the image below.

Go through each child and set its Material in the Sprite Renderer to the Additive material we created earlier. Assign each child the appropriate sprite. (The two caps should get the cap sprite and the middle segment should get the line sprite.)

Click on the LineJS object so that you can see the script in the Inspector panel. Assign the children to their appropriate slots and then drag the LineJS object into the Prefabs folder to create a prefab for it. You can now delete the LineJS object from the Hierarchy panel.

Step 2: Create Jagged Lines

Lightning tends to form jagged lines, so we'll need an algorithm to generate these. We'll do this by picking points at random along a line, and displacing them a random distance from the line.

Using a completely random displacement tends to make the line too jagged, so we'll smooth the results by limiting how far from each other neighbouring points can be displaced—see the difference between the second and third lines in the figure below.

We smooth the line by placing points at a similar offset to the previous point; this allows the line as a whole to wander up and down, while preventing any part of it from being too jagged.

Let's create a LightningBoltJS class to handle creating our jagged lines.

1
#pragma strict
2
import System.Collections.Generic;
3
class LightningBoltJS extends MonoBehaviour
4
{    
5
    //List of all of our active/inactive lines

6
    public var ActiveLineObj : List.<GameObject>;
7
    public var InactiveLineObj : List.<GameObject>;
8
    
9
    //Prefab for a line

10
    public var LinePrefab : GameObject;
11
    
12
    //Transparency

13
    public var Alpha : float;
14
    
15
    //The speed at which our bolts will fade out

16
    public var FadeOutRate : float;
17
    
18
    //The color of our bolts

19
    public var Tint : Color;
20
    
21
    //The position where our bolt started

22
    public function Start()
23
    {
24
        var first : GameObject = ActiveLineObj[0];
25
        return first.GetComponent(LineJS).A;
26
    }
27
    
28
    //The position where our bolt ended

29
    public function End()
30
    {
31
        var last : GameObject = ActiveLineObj[ActiveLineObj.Count-1];
32
        return last.GetComponent(LineJS).B;
33
    }
34
    
35
    //True if the bolt has completely faded out

36
    public function IsComplete()
37
    { 
38
	    return Alpha <= 0;
39
    }
40
    
41
    public function Initialize(maxSegments : int)
42
    {
43
        //Initialize lists for pooling

44
        ActiveLineObj = new List.<GameObject>();
45
        InactiveLineObj = new List.<GameObject>();
46
        
47
        for(var i : int = 0; i < maxSegments; i++)
48
        {
49
            //instantiate from our Line Prefab

50
            var line : GameObject = GameObject.Instantiate(LinePrefab);
51
            
52
            //parent it to our bolt object

53
            line.transform.parent = transform;
54
            
55
            //set it inactive

56
            line.SetActive(false);
57
            
58
            //add it to our list

59
            InactiveLineObj.Add(line);
60
        }
61
    }
62
    
63
    public function ActivateBolt(source : Vector2, dest : Vector2, color : Color, thickness : float)
64
    {
65
        //for use in loops later

66
        var i : int;
67
        
68
        //Store tint

69
        Tint = color;
70
        
71
        //Store alpha

72
        Alpha = 1.5f;
73
        
74
        //Store fade out rate

75
        FadeOutRate = 0.03f;
76
        
77
        //actually create the bolt

78
        //Prevent from getting a 0 magnitude

79
        if(Vector2.Distance(dest, source) <= 0)
80
        {
81
            var adjust : Vector2 = Random.insideUnitCircle;
82
            if(adjust.magnitude <= 0) adjust.x += .1f;
83
            dest += adjust;
84
        }
85
        
86
        //difference from source to destination

87
        var slope : Vector2 = dest - source;
88
        var normal : Vector2 = (new Vector2(slope.y, -slope.x)).normalized;
89
        
90
        //distance between source and destination

91
        var distance : float = slope.magnitude;
92
        
93
        var positions : List.<float> = new List.<float>();
94
        positions.Add(0);
95
        
96
        for (i = 0; i < distance / 4; i++) 
97
        {
98
            //Generate random positions between 0 and 1 to break up the bolt

99
            //positions.Add (Random.Range(0f, 1f));

100
            positions.Add(Random.Range(.25f, .75f));
101
        }
102
        
103
        positions.Sort();
104
        
105
        var Sway : float = 80;
106
        var Jaggedness : float = 1 / Sway;
107
        
108
        //Affects how wide the bolt is allowed to spread

109
        var spread : float = 1f;
110
        
111
        //Start at the source

112
        var prevPoint : Vector2 = source;
113
        
114
        //No previous displacement, so just 0

115
        var prevDisplacement : float = 0;
116
        
117
        for (i = 1; i < positions.Count; i++)
118
        {
119
            //don't allow more than we have in the pool

120
            var inactiveCount : int = InactiveLineObj.Count;
121
            if(inactiveCount <= 0) break;
122
            
123
            var pos : float = positions[i];
124
            var prevPos : float = positions[i - 1];
125
            //used to prevent sharp angles by ensuring very close positions also have small perpendicular variation.

126
            var scale : float = (distance * Jaggedness) * (pos - prevPos);
127
            
128
            //defines an envelope. Points near the middle of the bolt can be further from the central line.

129
            var envelope : float = pos > 0.95f ? 20 * (1 - pos) : spread;
130
            
131
            //calculate the displacement

132
            var displacement : float = Random.Range(-Sway, Sway);
133
            displacement -= (displacement - prevDisplacement) * (1 - scale);
134
            displacement *= envelope;
135
            
136
            //Calculate the end point

137
            var point : Vector2 = source + (pos * slope) + (displacement * normal);
138
            
139
            activateLine(prevPoint, point, thickness);
140
            prevPoint = point;
141
            prevDisplacement = displacement;
142
        }
143
        
144
        activateLine(prevPoint, dest, thickness);
145
    }
146
    
147
    public function DeactivateSegments()
148
    {
149
        for(var i : int = ActiveLineObj.Count - 1; i >= 0; i--)
150
        {
151
            var line : GameObject = ActiveLineObj[i];
152
            line.SetActive(false);
153
            
154
            ActiveLineObj.RemoveAt(i);
155
            InactiveLineObj.Add(line);
156
        }
157
    }
158
    
159
    function activateLine(A : Vector2, B : Vector2, thickness : float)
160
    {
161
        //get the inactive count

162
        var inactiveCount : int = InactiveLineObj.Count;
163
        
164
        //only activate if we can pull from inactive

165
        if(inactiveCount <= 0) return;
166
        
167
        //pull the GameObject

168
        var lineObj : GameObject = InactiveLineObj[InactiveLineObj.Count - 1];
169
        
170
        //set it active

171
        lineObj.SetActive(true);
172
        
173
        //get the Line component

174
        var lineComponent : LineJS = lineObj.GetComponent(LineJS);
175
        lineComponent.SetColor(Color.white);
176
        lineComponent.A = A;
177
        lineComponent.B = B;
178
        lineComponent.Thickness = thickness;
179
        ActiveLineObj.Add(lineObj);
180
        InactiveLineObj.Remove(lineObj);
181
    }
182
    
183
    public function Draw()
184
    {
185
        //if the bolt has faded out, no need to draw

186
        if (Alpha <= 0) return;
187
        
188
        for(var i : int = 0; i < ActiveLineObj.Count; i++)
189
        {
190
            var obj : GameObject = ActiveLineObj[i];
191
            var lineComponent : LineJS = obj.GetComponent(LineJS);
192
            lineComponent.SetColor(Tint * (Alpha * 0.6f));
193
            lineComponent.Draw();
194
        }
195
    }
196
    
197
    public function Update()
198
    {
199
        Alpha -= FadeOutRate;
200
    }
201
    
202
    //...

203
}

The code may look a bit intimidating, but it's not so bad once you understand the logic. Before we continue on, understand that we've chosen to pool our line segments in the bolts (since constantly instantiating and destroying objects can be costly in Unity).

  • The Initialize() function will be called once on each lightning bolt and will determine how many line segments each bolt is allowed to use.
  • The activateLine() function will activate a line segment using the given position data.
  • The DeactivateSegments() function will deactivate any active line segments in our bolt.
  • The ActivateBolt() function will handle creating our jagged lines and will call the activateLine() function to activate our line segments at the appropriate positions.

To create our jagged lines, we start by computing the slope between our two points, as well as the normal vector to that slope. We then choose a number of random positions along the line and store them in our positions list. We scale these positions between 0 and 1, such that 0 represents the start of the line and 1 represents the end point., and then sort these positions to allow us to easily add line segments between them.

The loop goes through the randomly chosen points and displaces them along the normal by a random amount. The scale factor is there to avoid overly sharp angles, and theenvelope ensures the lightning actually goes to the destination point by limiting displacement when we're close to the end. The spread is to assist in controlling how far the segments deviate from the slope of our line; a spread of 0 will essentially give you a straight line.

So, like we did with our LineJS class, let's make this into a prefab. From the menu, select GameObject > Create Empty. The object will appear in your Hierarchy panel. Rename it to BoltJS, and drag a copy of the LightningBoltJS script onto it. Finally, click on the BoltJS object and assign the LineJS prefab, from the Prefabs folder, to the appropriate slot in the LightningBoltJS script. Once you're done with that, simply drag the BoltJS object into the Prefabs folder to create a prefab.

Step 3: Add Animation

Lightning should flash brightly and then fade out. This is what our Update() and Draw() functions in LightningBoltJS are for. Calling Update() will make the bolt fade out. Calling Draw() will update the bolt's color on the screen. IsComplete() will tell you when the bolt has fully faded out.

Step 4: Create a Bolt

Now that we have our LightningBoltJS class, let's actually put it to good use and set up a quick demo scene.

We're going to use an object pool for this demo, so we'll want to create an empty object to hold our active and inactive bolts (simply for organizational purposes). In Unity, from the menu, select GameObject > Create Empty. The object will appear in your Hierarchy panel. Rename it to LightningPoolHolder.

Right click on the Scripts folder and select Create > Javascript. Name your script DemoScriptJS and open it. Here's some quick code to get you started:

1
#pragma strict
2
import System.Collections.Generic;
3
4
class DemoScriptJS extends MonoBehaviour
5
{
6
    //Prefabs to be assigned in Editor

7
    public var BoltPrefab : GameObject;
8
    
9
    //For pooling

10
    var activeBoltsObj : List.<GameObject>;
11
    var inactiveBoltsObj : List.<GameObject>;
12
    var maxBolts : int = 1000;
13
    
14
    //For handling mouse clicks

15
    var clicks : int = 0;
16
    var pos1 : Vector2;
17
    var pos2 : Vector2;
18
    
19
    function Start()
20
    {
21
        //Initialize lists

22
        activeBoltsObj = new List.<GameObject>();
23
        inactiveBoltsObj = new List.<GameObject>();
24
        
25
        //for use later

26
        var tempV3 : Vector3;
27
        
28
        //Grab the parent we'll be assigning to our bolt pool

29
        var p : GameObject = GameObject.Find("LightningPoolHolder");
30
        
31
        //For however many bolts we've specified

32
        for(var i : int = 0; i < maxBolts; i++)
33
        {
34
            //create from our prefab

35
            var bolt : GameObject = Instantiate(BoltPrefab);
36
            
37
            //Assign parent

38
            bolt.transform.parent = p.transform;
39
            
40
            //Initialize our lightning with a preset number of max sexments

41
            bolt.GetComponent(LightningBoltJS).Initialize(25);
42
            
43
            //Set inactive to start

44
            bolt.SetActive(false);
45
            
46
            //Store in our inactive list

47
            inactiveBoltsObj.Add(bolt);
48
        }
49
    }
50
    
51
    function Update()
52
    {
53
        //Declare variables for use later

54
        var boltObj : GameObject;
55
        var boltComponent : LightningBoltJS;
56
        var i : int;
57
        var tempV3 : Vector3;
58
        var adjust : Vector2;
59
        
60
        //store off the count for effeciency

61
        var activeLineCount : int = activeBoltsObj.Count;
62
        
63
        //loop through active lines (backwards because we'll be removing from the list)

64
        for (i = activeLineCount - 1; i >= 0; i--)
65
        {
66
        	//pull GameObject

67
        	boltObj = activeBoltsObj[i];
68
        
69
        	//get the LightningBolt component

70
        	boltComponent = boltObj.GetComponent(LightningBoltJS);
71
        
72
        	//if the bolt has faded out

73
        	if(boltComponent.IsComplete())
74
        	{
75
                //deactive the segments it contains

76
                boltComponent.DeactivateSegments();
77
                
78
                //set it inactive

79
                boltObj.SetActive(false);
80
                
81
                //move it to the inactive list

82
                activeBoltsObj.RemoveAt(i);
83
                inactiveBoltsObj.Add(boltObj);
84
        	}
85
        }
86
        
87
        //If left mouse button pressed

88
        if(Input.GetMouseButtonDown(0))
89
        {
90
            //if first click

91
            if(clicks == 0)
92
            {
93
                //store starting position

94
                tempV3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
95
                pos1 = new Vector2(tempV3.x, tempV3.y);
96
            }
97
            else if(clicks == 1) //second click

98
            {
99
                //store end position

100
                tempV3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
101
                pos2 = new Vector2(tempV3.x, tempV3.y);
102
                
103
                CreatePooledBolt(pos1,pos2, Color.white, 1f);
104
            }
105
            
106
            //increment our tick count

107
            clicks++;
108
            
109
            //restart the count after 2 clicks

110
            if(clicks > 1) clicks = 0;
111
        }
112
        
113
        //update and draw active bolts

114
        for(i = 0; i < activeBoltsObj.Count; i++)
115
        {
116
            boltObj = activeBoltsObj[i];
117
            boltObj.GetComponent(LightningBoltJS).Update();
118
            boltObj.GetComponent(LightningBoltJS).Draw();
119
        }
120
    }
121
    
122
    function CreatePooledBolt(source : Vector2, dest : Vector2, color : Color, thickness : float)
123
    {
124
        //if there is an inactive bolt to pull from the pool

125
        if(inactiveBoltsObj.Count > 0)
126
        {
127
            //pull the GameObject

128
            var boltObj : GameObject = inactiveBoltsObj[inactiveBoltsObj.Count - 1];
129
            
130
            //set it active

131
            boltObj.SetActive(true);
132
            
133
            //move it to the active list

134
            activeBoltsObj.Add(boltObj);
135
            inactiveBoltsObj.RemoveAt(inactiveBoltsObj.Count - 1);
136
            
137
            //get the bolt component

138
            var boltComponent : LightningBoltJS =  boltObj.GetComponent(LightningBoltJS);
139
            
140
            //activate the bolt using the given position data

141
            boltComponent.ActivateBolt(source, dest, color, thickness);
142
        }
143
    }
144
}

All this code does is give us a way to create bolts using object pooling. There are other ways you can set this up, but this is what we're going with! Once we've set it up, all you'll have to do is click twice to create a bolt on the screen: once for the start position and once for the end position.

We'll need an object to put our DemoScriptJS on. From the menu, select GameObject > Create Empty. The object will appear in your Hierarchy panel. Rename it to DemoScript and drag your DemoScriptJS script onto it. Click on the DemoScript object so we can view it in the Inspector panel. Assign the BoltJS prefab, from the Prefabs folder, to the matching slot in the DemoScriptJS.

That should be enough to get you going! Run the scene in Unity and try it out!

Step 5: Create Branch Lightning

You can use the LightningBoltJS class as a building block to create more interesting lightning effects. For example, you can make the bolts branch out as shown below:

To make the lightning branch, we pick random points along the lightning bolt and add new bolts that branch out from these points. In the code below, we create between three and six branches which separate from the main bolt at 30° angles.

1
#pragma strict

2
import System.Collections.Generic;
3
4
class BranchLightningJS extends MonoBehaviour
5
{
6
    //For holding all of our bolts in our branch

7
    public var boltsObj : List.<GameObject>;
8
    
9
    //If there are no bolts, then the branch is complete (we're not pooling here, but you could if you wanted)

10
    public function IsComplete()
11
    { 
12
        return boltsObj.Count <= 0;
13
    }
14
    
15
    //Start position of branch

16
    public var Start : Vector2;
17
    
18
    //End position of branch

19
    public var End : Vector2;
20
    
21
    static var rand : Random = new Random();
22
    
23
    public function Initialize(start : Vector2, end : Vector2, boltPrefab : GameObject)
24
    {
25
        //for use lateer

26
        var i : int;
27
        
28
        //store start and end positions

29
        Start = start;
30
        End = end;
31
        
32
        //create the  main bolt from our bolt prefab

33
        var mainBoltObj : GameObject = GameObject.Instantiate(boltPrefab);
34
        
35
        //get the LightningBolt component

36
        var mainBoltComponent : LightningBoltJS = mainBoltObj.GetComponent(LightningBoltJS);
37
        
38
        //initialize our bolt with a max of 5 segments

39
        mainBoltComponent.Initialize(5);
40
        
41
        //activate the bolt with our position data

42
        mainBoltComponent.ActivateBolt(start, end, Color.white, 1f);
43
        
44
        //add it to our list

45
        boltsObj.Add(mainBoltObj);
46
        
47
        //randomly determine how many sub branches there will be (3-6)

48
        var numBranches : int = Random.Range(3,6);
49
        
50
        //calculate the difference between our start and end points

51
        var diff : Vector2 = end - start;
52
        
53
        // pick a bunch of random points between 0 and 1 and sort them

54
        var branchPoints : List.<float> = new List.<float>();
55
        for(i = 0; i < numBranches; i++) branchPoints.Add(Random.value);
56
        branchPoints.Sort();
57
        
58
        //go through those points

59
        for (i = 0; i < branchPoints.Count; i++)
60
        {
61
            // Bolt.GetPoint() gets the position of the lightning bolt based on the percentage passed in (0 = start of bolt, 1 = end)

62
            var boltStart : Vector2 = mainBoltComponent.GetPoint(branchPoints[i]);
63
            
64
            //get rotation of 30 degrees. Alternate between rotating left and right. (i & 1 will be true for all odd numbers...yay bitwise operators!)

65
            var rot : Quaternion = Quaternion.AngleAxis(30 * ((i & 1) == 0 ? 1 : -1), new Vector3(0,0,1));
66
            
67
            var point : float = branchPoints[i];
68
            
69
            //calculate how much to adjust for our end position

70
            var adjust : Vector2 = rot * (Random.Range(.5f, .75f) * diff * (1 - point));
71
            
72
            //get the end position

73
            var boltEnd : Vector2 = adjust + boltStart;
74
            
75
            //instantiate from our bolt prefab

76
            var boltObj : GameObject = GameObject.Instantiate(boltPrefab);
77
            
78
            //get the LightningBolt component

79
            var boltComponent : LightningBoltJS = boltObj.GetComponent(LightningBoltJS);
80
            
81
            //initialize our bolt with a max of 5 segments

82
            boltComponent.Initialize(5);
83
            
84
            //activate the bolt with our position data

85
            boltComponent.ActivateBolt(boltStart, boltEnd, Color.white, 1f);
86
            
87
            //add it to the list

88
            boltsObj.Add(boltObj);
89
        }
90
    }
91
    
92
    public function Update()
93
    {
94
        //go through our active bolts

95
        for (var i : int = boltsObj.Count - 1; i >= 0; i--)
96
        {
97
            //get the GameObject

98
            var boltObj : GameObject = boltsObj[i];
99
            
100
            //get the LightningBolt component

101
            var boltComp : LightningBoltJS = boltObj.GetComponent(LightningBoltJS);
102
            
103
            //update/fade out the bolt

104
            boltComp.Update();
105
            
106
            //if the bolt has faded

107
            if(boltComp.IsComplete())
108
            {
109
                //remove it from our list

110
                boltsObj.RemoveAt(i);
111
                
112
                //destroy it (would be better to pool but I'll let you figure out how to do that =P)

113
                Destroy(boltObj);
114
            }
115
        }
116
    }
117
    
118
    //Draw our active bolts on screen

119
    public function Draw()
120
    {
121
        var boltObj : GameObject;
122
        for(var i : int; i < boltsObj.Count; i++)
123
        {
124
            boltObj = boltsObj[i];
125
            boltObj.GetComponent(LightningBoltJS).Draw();
126
        }
127
    }
128
}

This code works very similarly to our LightningBoltJS class with the exception that it does not use object pooling. Calling Initialize() is all you will need to do to create a branching bolt; after that, you will just need to call Update() and Draw(). I'll show you exactly how to do this in our DemoScriptJS later on in the tutorial.

You may have noticed the reference to a GetPoint() function in the LightningBoltJS class. We haven't actually implemented that function yet, so let's take care of that now.

Add the following function in the bottom of the LightningBoltJS class:

1
// Returns the point where the bolt is at a given fraction of the way through the bolt. Passing

2
// zero will return the start of the bolt, and passing 1 will return the end.

3
public function GetPoint(position : float)
4
{
5
    var start : Vector2 = Start();
6
    var length : float = Vector2.Distance(start, End());
7
    var dir : Vector2 = (End() - start) / length;
8
    position *= length;
9
    
10
    var line : LineJS;
11
    
12
    //find the appropriate line

13
    for(var i : int = 0; i < ActiveLineObj.Count; i++)
14
    {
15
        var x : GameObject = ActiveLineObj[i];
16
        
17
        if(Vector2.Dot(x.GetComponent(LineJS).B - start, dir) >= position)
18
        {
19
            line = x.GetComponent(LineJS);
20
            break;
21
        }
22
    }
23
    var lineStartPos : float = Vector2.Dot(line.A - start, dir);
24
    var lineEndPos : float = Vector2.Dot(line.B - start, dir);
25
    var linePos : float = (position - lineStartPos) / (lineEndPos - lineStartPos);
26
    
27
    return Vector2.Lerp(line.A, line.B, linePos);
28
}

Step 6: Create Lightning Text

Below is a video of another effect you can make out of the lightning bolts:

We'll need to do a little more setup for this one. First, from the Project panel, select Create > RenderTexture. Rename it to RenderText and set its Size to 256x256px. (It doesn't necessarily have to be that exact size, but the smaller it is, the faster the program will run.)

From the menu, select Edit > Project Settings > Tags and Layers. Then, in the Inspector panel, expand the Layers drop down and add Text into User Layer 8.

We'll then need to create a second camera. From the menu, select GameObject > Create Other > Camera. Rename it to TextCamera, and set its Projection to Orthographic and its Clear Flags to Solid Color. Set its Background color to(R: 0, G: 0, B: 0, A: 0) and set its Culling Mask to only be Text (the layer we just created). Finally, set its Target Texture to RenderText (the RenderTexture we created earlier). You'll probably need to play around with the camera's Size later, in order to get everything to fit on the screen.

Now we'll need to create the actual text we'll be drawing with our lightning. From the menu select GameObject > Create Other > GUI Text. Select the GUI Text object from the Hierarchy panel and set its Text to LIGHTNING, its Anchor to middle center, and its Alignment to center. Then, set its Layer to the Text layer we created earlier. You'll probably have to play around with the Font Size in order to fit the text on the screen.

Now select the Main Camera and set its Culling Mask to be everything but our Text layer. This will cause our GUI Text to apparently disappear from the screen, but it should be drawn on the RenderTexture we created earlier: select RenderText from the Project panel and you should be able to see the word LIGHTNING on the preview on the bottom of the panel.

If you can't see the word LIGHTNING, you'll need to play around with your positioning, font size, and (text) camera size. To help you position your text, click on TextCamera in the Hierarchy panel, and set its Target Texture to None. You'll now be able to see your GUI Text if you center it on the TextCamera. Once you have everything positioned, set the TextCamera's Target Texture back to RenderText.

Now for the code! We'll need to get the pixels from the text that we're drawing. We can do this by drawing our text to a RenderTarget and reading back the pixel data into aTexture2D with Texture2D.ReadPixels(). Then, we can store the coordinates of the pixels from the text as a List.<Vector2>.

Here's the code to do that:

1
//Capture the important points of our text for later

2
function TextCapture()
3
{
4
    //must wait until end of frame so something is actually drawn or else it will error

5
    yield WaitForEndOfFrame();
6
    
7
    //get the camera that draws our text

8
    var cam : Camera = GameObject.Find("TextCamera").GetComponent(Camera);
9
    
10
    //make sure it has an assigned RenderTexture

11
    if(cam.targetTexture != null) 
12
    {
13
        //pull the active RenderTexture

14
        RenderTexture.active = cam.targetTexture;
15
        
16
        //capture the image into a Texture2D

17
        var image : Texture2D = new Texture2D(cam.targetTexture.width, cam.targetTexture.height);
18
        image.ReadPixels(new Rect(0, 0, cam.targetTexture.width, cam.targetTexture.height), 0, 0);
19
        image.Apply();
20
        
21
        //calculate how the text will be scaled when it is displayed as lightning on the screen

22
        scaleText = 1 / (cam.ViewportToWorldPoint(new Vector3(1,0,0)).x - cam.ViewportToWorldPoint(Vector3.zero).x);
23
        
24
        //calculate how the text will be positioned when it is displayed as lightning on the screen (centered)

25
        positionText.x -= image.width * scaleText * .5f;
26
        positionText.y -= image.height * scaleText * .5f;
27
        
28
        //basically determines how many pixels we skip/check

29
        var interval : int = 2;
30
        
31
        //loop through pixels

32
        for(var y : int = 0; y < image.height; y += interval)
33
        {
34
            for(var x : int = 0; x < image.width; x += interval)
35
            {
36
                //get the color of the pixel

37
                var color : Color = image.GetPixel(x,y);
38
                
39
                //if the color has an r (red) value

40
                if(color.r > 0)
41
                {
42
                    //add it to our points for drawing

43
                    textPoints.Add(new Vector2(x,y));
44
                }
45
            }
46
        }
47
    }
48
}

Note: We'll have to run this function as a Coroutine at the start of our program in order for it to run correctly.

After that, each frame, we can randomly pick pairs of these points and create a lightning bolt between them. We want to design it so that the closer two points are to one another, the greater the chance is that we create a bolt between them.

There's a simple technique we can use to accomplish this: we'll pick the first point at random, and then we'll pick a fixed number of other points at random and choose the nearest.

Here's the code for that (we'll add it to our DemoScriptJS later):

1
//go through the points we capture earlier

2
for (var i1 : int = 0; i1 < textPoints.Count; i1++)
3
{
4
    var point : Vector2 = textPoints[i1];
5
    //randomly ignore certain points

6
    if(Random.Range(0,75) != 0) continue;
7
    
8
    //placeholder values

9
    var nearestParticle : Vector2 = Vector2.zero;
10
    var nearestDistSquared : float = float.MaxValue;
11
    
12
    for (i = 0; i < 50; i++)
13
    {
14
        //select a random point

15
        var other : Vector2 = textPoints[Random.Range(0, textPoints.Count)];
16
        
17
        //calculate the distance (squared for performance benefits) between the two points

18
        var distSquared : float = DistanceSquared(point, other);
19
        
20
        //If this point is the nearest point (but not too near!)

21
        if (distSquared < nearestDistSquared && distSquared > 3 * 3)
22
        {
23
            //store off the data

24
            nearestDistSquared = distSquared;
25
            nearestParticle = other;
26
        }
27
    }
28
    
29
    //if the point we found isn't too near/far

30
    if (nearestDistSquared < 25 * 25 && nearestDistSquared > 3 * 3)
31
    {
32
        //create a (pooled) bolt at the corresponding screen position

33
        CreatePooledBolt((point * scaleText) + positionText, (nearestParticle * scaleText) + positionText, new Color(Random.value,Random.value,Random.value,1f), 1f);
34
    }
35
}
36
37
/* The code above uses the following function 

38
 * It'll need to be placed appropriately

39
--------------------------------------------- 

40
//calculate distance squared (no square root = performance boost)

41
public function DistanceSquared(a : Vector2, b : Vector2)

42
{

43
    return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));

44
}

45
---------------------------------------------*/

The number of candidate points we test will affect the look of the lightning text; checking a larger number of points will allow us to find very close points to draw bolts between, which will make the text very neat and legible, but with fewer long lightning bolts between letters. Smaller numbers will make the lightning text look wilder but less legible.

Step 7: Try Other Variations

We've discussed making branch lightning and lightning text, but those certainly aren't the only effects you can make. Let's look at a couple other variations on lightning you may want to use.

Moving Lightning

You may often want to make a moving bolt of lightning. You can do this by adding a new short bolt each frame at the end point of the previous frame's bolt.

1
//Will contain all of the pieces for the moving bolt

2
var movingBolt : List.<GameObject>;
3
4
//used for actually moving the moving bolt

5
var lightningEnd : Vector2 = new Vector2(100, 100);
6
var lightningVelocity : Vector2 = new Vector2(1, 0);
7
8
function Update()
9
{
10
    //loop through all of our bolts that make up the moving bolt

11
    for(i = movingBolt.Count - 1; i >= 0; i--)
12
    {
13
        boltObj = movingBolt[i];
14
        //get the bolt component

15
        boltComponent = boltObj.GetComponent(LightningBoltJS);
16
        
17
        //if the bolt has faded out

18
        if(boltComponent.IsComplete())
19
        {
20
            //destroy it

21
            Destroy(movingBolt[i]);
22
            
23
            //remove it from our list

24
            movingBolt.RemoveAt(i);
25
            
26
            //on to the next one, on on to the next one

27
            continue;
28
        }
29
        
30
        //update and draw bolt

31
        boltComponent.Update();
32
        boltComponent.Draw();
33
    }
34
    
35
    //if our moving bolt is active

36
    if(movingBolt.Count > 0)
37
    {
38
        boltObj = movingBolt[movingBolt.Count-1];
39
        //calculate where it currently ends

40
        lightningEnd = boltObj.GetComponent(LightningBoltJS).End();
41
        
42
        //if the end of the bolt is within 25 units of the camera

43
        if(Vector2.Distance(lightningEnd,Camera.main.transform.position) < 25)
44
        {
45
            //instantiate from our bolt prefab

46
            boltObj = GameObject.Instantiate(BoltPrefab);
47
            
48
            //get the bolt component

49
            boltComponent = boltObj.GetComponent(LightningBoltJS);
50
            
51
            //initialize it with a maximum of 5 segments

52
            boltComponent.Initialize(5);
53
            
54
            //activate the bolt using our position data (from the current end of our moving bolt to the current end + velocity) 

55
            boltComponent.ActivateBolt(lightningEnd,lightningEnd + lightningVelocity, Color.white, 1f);
56
            
57
            //add it to our list

58
            movingBolt.Add(boltObj);
59
            
60
            //update and draw our new bolt

61
            boltComponent.Update();
62
            boltComponent.Draw();
63
        }
64
    }
65
}

Burst Lightning

This variation offers a dramatic effect that shoots lightning out in a circle from the centre point:

1
//get the difference between our two positions (destination - source = vector from source to destination)

2
var diff : Vector2 = pos2 - pos1;
3
4
function Update()
5
{
6
    //define how many bolts we want in our circle

7
    var boltsInBurst : int = 10;
8
    
9
    for(i = 0; i < boltsInBurst; i++)
10
    {
11
        //rotate around the z axis to the appropriate angle

12
        var rot : Quaternion = Quaternion.AngleAxis((360f/boltsInBurst) * i, new Vector3(0,0,1));
13
        
14
        adjust = rot * diff;
15
        
16
        //Calculate the end position for the bolt

17
        var boltEnd : Vector2 = adjust + pos1;
18
        
19
        //create a (pooled) bolt from pos1 to boltEnd

20
        CreatePooledBolt(pos1, boltEnd, Color.white, 1f);
21
    }
22
}

Step 8: Put It All Together in DemoScriptJS

You're going to want to be able to try out all of these fancy effects we've created so far, so let's put all of them into the DemoScriptJS we made earlier. You'll be able to toggle between effects by hitting the number keys on your keyboard to select the effect, and then just clicking twice like we did with our bolts before.

Here's the full code:

1
#pragma strict

2
#pragma strict

3
import System.Collections.Generic;
4
5
class DemoScriptJS extends MonoBehaviour
6
{
7
    //Prefabs to be assigned in Editor

8
    public var BoltPrefab : GameObject;
9
    public var BranchPrefab : GameObject;
10
    
11
    //For pooling

12
    var activeBoltsObj : List.<GameObject>;
13
    var inactiveBoltsObj : List.<GameObject>;
14
    var maxBolts : int = 1000;
15
    
16
    var scaleText : float;
17
    var positionText : Vector2;
18
    
19
    //Different modes for the demo

20
    class Mode
21
    {
22
        public static final var bolt : byte = 0;
23
        public static final var branch : byte = 1;
24
        public static final var moving : byte = 2;
25
        public static final var text : byte = 3;
26
        public static final var nodes : byte = 4;
27
        public static final var burst : byte = 5;
28
    }
29
    
30
    //The current mode the demo is in

31
    var currentMode : byte = Mode.bolt;
32
    
33
    //Will contain all of the pieces for the moving bolt

34
    var movingBolt : List.<GameObject>;
35
    
36
    //used for actually moving the moving bolt

37
    var lightningEnd : Vector2 = new Vector2(100, 100);
38
    var lightningVelocity : Vector2 = new Vector2(1, 0);
39
    
40
    //Will contain all of the pieces for the branches

41
    var branchesObj : List.<GameObject>;
42
    
43
    //For handling mouse clicks

44
    var clicks : int = 0;
45
    var pos1 : Vector2;
46
    var pos2 : Vector2;
47
    
48
    //For storing all of the pixels that need to be drawn by the bolts 

49
    var textPoints : List.<Vector2>;
50
    
51
    //true in text mode

52
    var shouldText : boolean = false;
53
    
54
    function Start()
55
    {
56
        //Initialize lists

57
        activeBoltsObj = new List.<GameObject>();
58
        inactiveBoltsObj = new List.<GameObject>();
59
        branchesObj = new List.<GameObject>();
60
        
61
        //for use later

62
        var tempV3 : Vector3;
63
        
64
        //Grab the parent we'll be assigning to our bolt pool

65
        var p : GameObject = GameObject.Find("LightningPoolHolder");
66
        
67
        //For however many bolts we've specified

68
        for(var i : int = 0; i < maxBolts; i++)
69
        {
70
            //create from our prefab

71
            var bolt : GameObject = Instantiate(BoltPrefab);
72
            
73
            //Assign parent

74
            bolt.transform.parent = p.transform;
75
            
76
            //Initialize our lightning with a preset number of max sexments

77
            bolt.GetComponent(LightningBoltJS).Initialize(25);
78
            
79
            //Set inactive to start

80
            bolt.SetActive(false);
81
            
82
            //Store in our inactive list

83
            inactiveBoltsObj.Add(bolt);
84
        }
85
        
86
        //Start up a coroutine to capture the pixels we'll be drawing from our text (need the coroutine or error)

87
        StartCoroutine(TextCapture());
88
    }
89
    
90
    function Update()
91
    {
92
        //Declare variables for use later

93
        var boltObj : GameObject;
94
        var boltComponent : LightningBoltJS;
95
        var i : int;
96
        var tempV3 : Vector3;
97
        var adjust : Vector2;
98
        var branchObj : GameObject;
99
        var branchComponent : BranchLightningJS;
100
        
101
        //store off the count for effeciency

102
        var activeLineCount : int = activeBoltsObj.Count;
103
        
104
        //loop through active lines (backwards because we'll be removing from the list)

105
        for (i = activeLineCount - 1; i >= 0; i--)
106
        {
107
            //pull GameObject

108
            boltObj = activeBoltsObj[i];
109
            
110
            //get the LightningBolt component

111
            boltComponent = boltObj.GetComponent(LightningBoltJS);
112
            
113
            //if the bolt has faded out

114
            if(boltComponent.IsComplete())
115
            {
116
                //deactive the segments it contains

117
                boltComponent.DeactivateSegments();
118
                
119
                //set it inactive

120
                boltObj.SetActive(false);
121
                
122
                //move it to the inactive list

123
                activeBoltsObj.RemoveAt(i);
124
                inactiveBoltsObj.Add(boltObj);
125
            }
126
        }
127
        
128
        //check for key press and set mode accordingly

129
        if(Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1))
130
        {
131
            shouldText = false;
132
            currentMode = Mode.bolt;
133
        }
134
        else if(Input.GetKeyDown(KeyCode.Alpha2) || Input.GetKeyDown(KeyCode.Keypad2))
135
        {
136
            shouldText = false;
137
            currentMode = Mode.branch;
138
        }
139
        else if(Input.GetKeyDown(KeyCode.Alpha3) || Input.GetKeyDown(KeyCode.Keypad3))
140
        {
141
            shouldText = false;
142
            currentMode = Mode.moving;
143
        }
144
        else if(Input.GetKeyDown(KeyCode.Alpha4) || Input.GetKeyDown(KeyCode.Keypad4))
145
        {
146
            shouldText = true;
147
            currentMode = Mode.text;
148
        }
149
        else if(Input.GetKeyDown(KeyCode.Alpha5) || Input.GetKeyDown(KeyCode.Keypad5))
150
        {
151
            shouldText = false;
152
            currentMode = Mode.nodes;
153
        }
154
        else if(Input.GetKeyDown(KeyCode.Alpha6) || Input.GetKeyDown(KeyCode.Keypad6))
155
        {
156
            shouldText = false;
157
            currentMode = Mode.burst;
158
        }
159
        
160
        //If left mouse button pressed

161
        if(Input.GetMouseButtonDown(0))
162
        {
163
            //if first click

164
            if(clicks == 0)
165
            {
166
                //store starting position

167
                tempV3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
168
                pos1 = new Vector2(tempV3.x, tempV3.y);
169
            }
170
            else if(clicks == 1) //second click

171
            {
172
                //store end position

173
                tempV3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
174
                pos2 = new Vector2(tempV3.x, tempV3.y);
175
                
176
                //Handle the current mode appropriately

177
                switch (currentMode)
178
                {
179
                    case Mode.bolt:
180
                        //create a (pooled) bolt from pos1 to pos2

181
                        CreatePooledBolt(pos1,pos2, Color.white, 1f);
182
                    break;
183
                    
184
                    case Mode.branch:
185
                        //instantiate from our branch prefab

186
                        branchObj = GameObject.Instantiate(BranchPrefab);
187
                        
188
                        //get the branch component

189
                        branchComponent = branchObj.GetComponent(BranchLightningJS);
190
                        
191
                        //initialize the branch component using our position data

192
                        branchComponent.Initialize(pos1, pos2, BoltPrefab);
193
                        
194
                        //add it to the list of active branches

195
                        branchesObj.Add(branchObj);
196
                    break;
197
                    
198
                    case Mode.moving:
199
                        //Prevent from getting a 0 magnitude (0 causes errors 

200
                        if(Vector2.Distance(pos1, pos2) <= 0)
201
                        {
202
                            //Try a random position

203
                            adjust = Random.insideUnitCircle;
204
                            
205
                            //failsafe

206
                            if(adjust.magnitude <= 0) adjust.x += .1f;
207
                            
208
                            //Adjust the end position

209
                            pos2 += adjust;
210
                        }
211
                        
212
                        //Clear out any old moving bolt (this is designed for one moving bolt at a time)

213
                        for(i = movingBolt.Count - 1; i >= 0; i--)
214
                        {
215
                            Destroy(movingBolt[i]);
216
                            movingBolt.RemoveAt(i);
217
                        }
218
                        
219
                        //get the "velocity" so we know what direction to send the bolt in after initial creation

220
                        lightningVelocity = (pos2 - pos1).normalized;
221
                        
222
                        //instantiate from our bolt prefab

223
                        boltObj = GameObject.Instantiate(BoltPrefab);
224
                        
225
                        //get the bolt component

226
                        boltComponent = boltObj.GetComponent(LightningBoltJS);
227
                        
228
                        //initialize it with 5 max segments

229
                        boltComponent.Initialize(5);
230
                        
231
                        //activate the bolt using our position data

232
                        boltComponent.ActivateBolt(pos1, pos2, Color.white, 1f);
233
                        
234
                        //add it to our list

235
                        movingBolt.Add(boltObj);
236
                    break;
237
                    
238
                    case Mode.burst:
239
                        //get the difference between our two positions (destination - source = vector from source to destination)

240
                        var diff : Vector2 = pos2 - pos1;
241
                        
242
                        //define how many bolts we want in our circle

243
                        var boltsInBurst : int = 10;
244
                        
245
                        for(i = 0; i < boltsInBurst; i++)
246
                        {
247
                            //rotate around the z axis to the appropriate angle

248
                            var rot : Quaternion = Quaternion.AngleAxis((360f/boltsInBurst) * i, new Vector3(0,0,1));
249
                            
250
                            adjust = rot * diff;
251
                            
252
                            //Calculate the end position for the bolt

253
                            var boltEnd : Vector2 = adjust + pos1;
254
                            
255
                            //create a (pooled) bolt from pos1 to boltEnd

256
                            CreatePooledBolt(pos1, boltEnd, Color.white, 1f);
257
                        }
258
                    
259
                    break;
260
                }
261
            }
262
            
263
            //increment our tick count

264
            clicks++;
265
            
266
            //restart the count after 2 clicks

267
            if(clicks > 1) clicks = 0;
268
        }
269
        
270
        //if in node mode

271
        if(currentMode == Mode.nodes)
272
        {
273
            //constantly create a (pooled) bolt between the two assigned positions

274
            CreatePooledBolt(pos1, pos2, Color.white, 1f);
275
        }
276
        
277
        //loop through any active branches

278
        for(i = branchesObj.Count - 1; i >= 0; i--)
279
        {
280
            branchObj = branchesObj[i];
281
            
282
            //pull the branch lightning component

283
            branchComponent = branchObj.GetComponent(BranchLightningJS);
284
            
285
            //If it's faded out already

286
            if(branchComponent.IsComplete())
287
            {
288
                //destroy it

289
                Destroy(branchesObj[i]);
290
                
291
                //take it out of our list

292
                branchesObj.RemoveAt(i);
293
                
294
                //move on to the next branch

295
                continue;
296
            }
297
            
298
            //draw and update the branch

299
            branchComponent.Update();
300
            branchComponent.Draw();
301
        }
302
        
303
        //loop through all of our bolts that make up the moving bolt

304
        for(i = movingBolt.Count - 1; i >= 0; i--)
305
        {
306
            boltObj = movingBolt[i];
307
            //get the bolt component

308
            boltComponent = boltObj.GetComponent(LightningBoltJS);
309
            
310
            //if the bolt has faded out

311
            if(boltComponent.IsComplete())
312
            {
313
                //destroy it

314
                Destroy(movingBolt[i]);
315
                
316
                //remove it from our list

317
                movingBolt.RemoveAt(i);
318
                
319
                //on to the next one, on on to the next one

320
                continue;
321
            }
322
            
323
            //update and draw bolt

324
            boltComponent.Update();
325
            boltComponent.Draw();
326
        }
327
        
328
        //if our moving bolt is active

329
        if(movingBolt.Count > 0)
330
        {
331
            boltObj = movingBolt[movingBolt.Count-1];
332
            //calculate where it currently ends

333
            lightningEnd = boltObj.GetComponent(LightningBoltJS).End();
334
            
335
            //if the end of the bolt is within 25 units of the camera

336
            if(Vector2.Distance(lightningEnd,Camera.main.transform.position) < 25)
337
            {
338
                //instantiate from our bolt prefab

339
                boltObj = GameObject.Instantiate(BoltPrefab);
340
                
341
                //get the bolt component

342
                boltComponent = boltObj.GetComponent(LightningBoltJS);
343
                
344
                //initialize it with a maximum of 5 segments

345
                boltComponent.Initialize(5);
346
                
347
                //activate the bolt using our position data (from the current end of our moving bolt to the current end + velocity) 

348
                boltComponent.ActivateBolt(lightningEnd,lightningEnd + lightningVelocity, Color.white, 1f);
349
                
350
                //add it to our list

351
                movingBolt.Add(boltObj);
352
                
353
                //update and draw our new bolt

354
                boltComponent.Update();
355
                boltComponent.Draw();
356
            }
357
        }
358
        
359
        //if in text mode

360
        if(shouldText)
361
        {
362
            //go through the points we capture earlier

363
            for (var i1 : int = 0; i1 < textPoints.Count; i1++)
364
            {
365
                var point : Vector2 = textPoints[i1];
366
                //randomly ignore certain points

367
                if(Random.Range(0,75) != 0) continue;
368
                
369
                //placeholder values

370
                var nearestParticle : Vector2 = Vector2.zero;
371
                var nearestDistSquared : float = float.MaxValue;
372
                
373
                for (i = 0; i < 50; i++)
374
                {
375
                    //select a random point

376
                    var other : Vector2 = textPoints[Random.Range(0, textPoints.Count)];
377
                    
378
                    //calculate the distance (squared for performance benefits) between the two points

379
                    var distSquared : float = DistanceSquared(point, other);
380
                    
381
                    //If this point is the nearest point (but not too near!)

382
                    if (distSquared < nearestDistSquared && distSquared > 3 * 3)
383
                    {
384
                        //store off the data

385
                        nearestDistSquared = distSquared;
386
                        nearestParticle = other;
387
                    }
388
                }
389
                
390
                //if the point we found isn't too near/far

391
                if (nearestDistSquared < 25 * 25 && nearestDistSquared > 3 * 3)
392
                {
393
                    //create a (pooled) bolt at the corresponding screen position

394
                    CreatePooledBolt((point * scaleText) + positionText, (nearestParticle * scaleText) + positionText, new Color(Random.value,Random.value,Random.value,1f), 1f);
395
                }
396
            }
397
        }
398
        
399
        //update and draw active bolts

400
        for(i = 0; i < activeBoltsObj.Count; i++)
401
        {
402
            boltObj = activeBoltsObj[i];
403
            boltObj.GetComponent(LightningBoltJS).Update();
404
            boltObj.GetComponent(LightningBoltJS).Draw();
405
        }
406
    }
407
    
408
    //calculate distance squared (no square root = performance boost)

409
    public function DistanceSquared(a : Vector2, b : Vector2)
410
    {
411
        return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
412
    }
413
    
414
    function CreatePooledBolt(source : Vector2, dest : Vector2, color : Color, thickness : float)
415
    {
416
        //if there is an inactive bolt to pull from the pool

417
        if(inactiveBoltsObj.Count > 0)
418
        {
419
            //pull the GameObject

420
            var boltObj : GameObject = inactiveBoltsObj[inactiveBoltsObj.Count - 1];
421
            
422
            //set it active

423
            boltObj.SetActive(true);
424
            
425
            //move it to the active list

426
            activeBoltsObj.Add(boltObj);
427
            inactiveBoltsObj.RemoveAt(inactiveBoltsObj.Count - 1);
428
            
429
            //get the bolt component

430
            var boltComponent : LightningBoltJS =  boltObj.GetComponent(LightningBoltJS);
431
            
432
            //activate the bolt using the given position data

433
            boltComponent.ActivateBolt(source, dest, color, thickness);
434
        }
435
    }
436
    
437
    //Capture the important points of our text for later

438
    function TextCapture()
439
    {
440
        //must wait until end of frame so something is actually drawn or else it will error

441
        yield WaitForEndOfFrame();
442
        
443
        //get the camera that draws our text

444
        var cam : Camera = GameObject.Find("TextCamera").GetComponent(Camera);
445
        
446
        //make sure it has an assigned RenderTexture

447
        if(cam.targetTexture != null) 
448
        {
449
            //pull the active RenderTexture

450
            RenderTexture.active = cam.targetTexture;
451
            
452
            //capture the image into a Texture2D

453
            var image : Texture2D = new Texture2D(cam.targetTexture.width, cam.targetTexture.height);
454
            image.ReadPixels(new Rect(0, 0, cam.targetTexture.width, cam.targetTexture.height), 0, 0);
455
            image.Apply();
456
            
457
            //calculate how the text will be scaled when it is displayed as lightning on the screen

458
            scaleText = 1 / (cam.ViewportToWorldPoint(new Vector3(1,0,0)).x - cam.ViewportToWorldPoint(Vector3.zero).x);
459
            
460
            //calculate how the text will be positioned when it is displayed as lightning on the screen (centered)

461
            positionText.x -= image.width * scaleText * .5f;
462
            positionText.y -= image.height * scaleText * .5f;
463
            
464
            //basically determines how many pixels we skip/check

465
            var interval : int = 2;
466
            
467
            //loop through pixels

468
            for(var y : int = 0; y < image.height; y += interval)
469
            {
470
                for(var x : int = 0; x < image.width; x += interval)
471
                {
472
                    //get the color of the pixel

473
                    var color : Color = image.GetPixel(x,y);
474
                    
475
                    //if the color has an r (red) value

476
                    if(color.r > 0)
477
                    {
478
                        //add it to our points for drawing

479
                        textPoints.Add(new Vector2(x,y));
480
                    }
481
                }
482
            }
483
        }
484
    }
485
}

Conclusion

Lightning is a great special effect for sprucing up your games. The effects described in this tutorial are a nice starting point, but it's certainly not all you can do with lightning. With a bit of imagination you can make all kinds of awe-inspiring lightning effects! Download the source code and experiment on your own.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.