1
+ /*
2
+ This script is placed in public domain. The author takes no responsibility for any possible harm.
3
+ Contributed by Jonathan Czeck
4
+ */
5
+ using UnityEngine ;
6
+ using UnitySteer . Vehicles ;
7
+ using System . Collections ;
8
+
9
+ public class LightningBolt : MonoBehaviour
10
+ {
11
+ public Transform target ;
12
+ public int zigs = 100 ;
13
+ public float speed = 1f ;
14
+ public float scale = 1f ;
15
+ public Light startLight ;
16
+ public Light endLight ;
17
+
18
+ Perlin noise ;
19
+ float oneOverZigs ;
20
+
21
+ private Particle [ ] particles ;
22
+ private Rope [ ] vehicles ;
23
+
24
+ void Start ( )
25
+ {
26
+ oneOverZigs = 1f / ( float ) zigs ;
27
+ particleEmitter . emit = false ;
28
+
29
+ particleEmitter . Emit ( zigs ) ;
30
+ particles = particleEmitter . particles ;
31
+ vehicles = new Rope [ particles . Length ] ;
32
+ RandomizeParticlePositions ( ) ;
33
+
34
+ for ( int i = 0 ; i < particles . Length ; i ++ )
35
+ {
36
+ // Transform t = new Transform();
37
+ vehicles [ i ] = new Rope ( particles [ i ] . position , 0.1f , null , null ) ;
38
+
39
+ vehicles [ i ] . Mass = 0.1f ;
40
+ vehicles [ i ] . Radius = 0.05f ;
41
+ vehicles [ i ] . MaxSpeed = 5f ;
42
+ vehicles [ i ] . MaxForce = 10f ;
43
+ }
44
+ for ( int i = 0 ; i < particles . Length - 1 ; i ++ )
45
+ {
46
+ // Transform t = new Transform();
47
+ vehicles [ i ] . Next = vehicles [ i + 1 ] ;
48
+ if ( i > 0 )
49
+ {
50
+ vehicles [ i ] . Previous = vehicles [ i - 1 ] ;
51
+ }
52
+ }
53
+ }
54
+
55
+ void Update ( )
56
+ {
57
+ // RandomizeParticlePositions(true);
58
+ RandomizeParticlePositions ( particles . Length - 1 ) ;
59
+ // Randomize the position of the last particle only
60
+ vehicles [ particles . Length - 1 ] . Position = particles [ particles . Length - 1 ] . position ;
61
+ for ( int i = 0 ; i < particles . Length - 1 ; i ++ )
62
+ {
63
+ vehicles [ i ] . Position = particles [ i ] . position ;
64
+ vehicles [ i ] . update ( Time . time , Time . deltaTime ) ;
65
+ particles [ i ] . position = vehicles [ i ] . Position ;
66
+ }
67
+
68
+ particleEmitter . particles = particles ;
69
+
70
+ if ( particleEmitter . particleCount >= 2 )
71
+ {
72
+ if ( startLight )
73
+ startLight . transform . position = particles [ 0 ] . position ;
74
+ if ( endLight )
75
+ endLight . transform . position = particles [ particles . Length - 1 ] . position ;
76
+ }
77
+ }
78
+
79
+ void RandomizeParticlePositions ( )
80
+ {
81
+ RandomizeParticlePositions ( 0 , false ) ;
82
+ }
83
+
84
+ void RandomizeParticlePositions ( int startIndex )
85
+ {
86
+ RandomizeParticlePositions ( startIndex , false ) ;
87
+ }
88
+
89
+ void RandomizeParticlePositions ( bool isOffset )
90
+ {
91
+ RandomizeParticlePositions ( 0 , isOffset ) ;
92
+ }
93
+
94
+ void RandomizeParticlePositions ( int startIndex , bool isOffset )
95
+ {
96
+ if ( noise == null )
97
+ noise = new Perlin ( ) ;
98
+
99
+ float timex = Time . time * speed * 0.1365143f ;
100
+ float timey = Time . time * speed * 1.21688f ;
101
+ float timez = Time . time * speed * 2.5564f ;
102
+ /*
103
+ float timey = Time.time * speed * 0.21688f;
104
+ float timez = Time.time * speed * 0.2564f;
105
+ */
106
+
107
+ for ( int i = startIndex ; i < particles . Length ; i ++ )
108
+ {
109
+ Vector3 position = isOffset ? particles [ i ] . position
110
+ : Vector3 . Lerp ( transform . position , target . position , oneOverZigs * ( float ) i ) ;
111
+ Vector3 offset = new Vector3 ( noise . Noise ( timex + position . x , timex + position . y , timex + position . z ) ,
112
+ noise . Noise ( timey + position . x , timey + position . y , timey + position . z ) ,
113
+ noise . Noise ( timez + position . x , timez + position . y , timez + position . z ) ) ;
114
+ position += ( offset * scale * ( ( float ) i * oneOverZigs ) ) ;
115
+
116
+ particles [ i ] . position = position ;
117
+ particles [ i ] . color = Color . white ;
118
+ particles [ i ] . energy = 1f ;
119
+ // Debug.Log("new pos for "+i+" "+position);
120
+ }
121
+ }
122
+ }
0 commit comments