Search Unity

[Ask] Suggestion for lighting bolt effect

Discussion in 'Scripting' started by beco13, Jul 23, 2011.

  1. beco13

    beco13

    Joined:
    May 31, 2011
    Posts:
    30
    Guys, here I'm developing a lightning bolt/thunder effect..

    The main idea I have is to create a lighting/thunder effect,
    1. create a line to reach the target position ( not very well implemented, somehow the tail are showing too fast, not slow enough)
    2. create the lightning/thunder effect ( well implemented)
    3. create some noise for the lightning effect to make it more real like a lightning/thunder effect.

    I'm using a trail renderer to draw its tail.

    1.Any idea about how to develop this even better?

    2.Or how to make my code faster? because when the peaks get bigger it's very lagging.

    3.Somehow, I want to add some noise to make it even more like a lighting. any suggestion for that?

    If any of you guys, like or want to use my code, please let me know 1st. thanks
    Code (csharp):
    1.  
    2. //number of peaks
    3. var thunderPeaks: int = 10;
    4. var startPoint  : Vector3;
    5. var targetVector    : Transform;
    6. var trailRenderer   : TrailRenderer;
    7. var midPoint    : Vector3;
    8. var endPoint    : Vector3;
    9. var currentPoint: Vector3;
    10. var nextPoint   : Vector3;
    11. var thunderIs   : boolean = false;
    12. var thunderDone : boolean = false;
    13. var thunderVelocity : int;
    14. private var peak : Vector3[];
    15. private var checkPath;
    16.     var count = false;
    17.  
    18. function Awake()
    19. {
    20.     startPoint = transform.position;
    21.     transform.position = startPoint;
    22.     endPoint = targetVector.position;
    23.     thunderIs = false;
    24.     peak = new Vector3[thunderPeaks - 1];
    25. }
    26.  
    27. function OnGUI()
    28. {
    29.     if(GUI.Button (Rect(0,0,100,20),"Again"))
    30.     {
    31.     thunderDone = false;
    32.     thunderIs = false;
    33.     count=  false; 
    34.     }  
    35. }
    36.  
    37. function Update()
    38. {
    39.     endPoint = targetVector.position;
    40.     Thunder();
    41. }
    42.  
    43. function Thunder()
    44. {
    45.     trailRenderer = GetComponentInChildren(TrailRenderer);
    46.     //put count path in awake
    47.         Pillar(function(){});
    48.         CountPath();
    49.  
    50.         //set the object position
    51.         MoveThunder(function(){});
    52. }
    53.  
    54. function Pillar(y)
    55. {
    56.  
    57. if(!thunderIs  !thunderDone)
    58. {
    59. trailRenderer.time = 1.5;
    60. if(transform.position != startPoint)transform.position = startPoint;
    61. transform.position = midPoint /2 ;
    62. print("tp"+transform.position);
    63. yield WaitForSeconds(0.5 * thunderVelocity);
    64. transform.position = endPoint;
    65. print("tp2"+transform.position);
    66. yield WaitForSeconds(0.5 * thunderVelocity);
    67.     if(y)
    68.     {
    69.         y();
    70.     }
    71. thunderIs = true;
    72. }
    73. }
    74.  
    75. function CountPath()
    76. {
    77.     midPoint = endPoint - startPoint;
    78.     trailRenderer.time = 0.41;
    79.     var distX : float = midPoint.x / thunderPeaks;
    80.     var distY : float = midPoint.y / thunderPeaks;
    81.     var distZ : float = midPoint.z / thunderPeaks; 
    82.     print("dist" + distX);
    83.     print("dist" + distY);
    84.     print("dist" + distZ);
    85.     if(midPoint == 0) midPoint = Vector3(1,1,1);
    86.     print("start" + startPoint);
    87.     print("end " + endPoint);
    88.     print("half" + midPoint / 2);
    89.     for (var i = 0 ; i < thunderPeaks ; i++)
    90.     {
    91.         for(var j = 0; j < peak.length; j+=2)
    92.         {
    93.                 peak[j].x = startPoint.x + (peak.length/3) * (midPoint.x / (peak.length + 1) / 2) + distX * j;
    94.                 peak[j].y = startPoint.y + (midPoint.y / (peak.length + 1) / 2) + distY * j;
    95.                 peak[j].z = startPoint.z + (peak.length/3) * (midPoint.z / (peak.length + 1) / 2) + distZ * j ;
    96.         print("peak" + j + peak[j]);
    97.         }
    98.         for(var k = 1; k < peak.length; k+=2)
    99.         {
    100.         peak[k].x = startPoint.x - (midPoint.x / (peak.length + 1) / 2 ) + distX * k;
    101.         peak[k].y = startPoint.y + (midPoint.y / (peak.length + 1) / 2) + distY * k;
    102.         peak[k].z = startPoint.z - (midPoint.z / (peak.length + 1) / 2 ) + distZ * k;
    103.         }
    104.     }
    105. }
    106.  
    107. function MoveThunder(x)
    108. {
    109.         if(!count  thunderIs)
    110.         {
    111.         renderer.enabled = true;
    112.         transform.position = startPoint;
    113.         for (var l = 0; l < peak.length; l++)
    114.         {
    115.             currentPoint = transform.position;
    116.             nextPoint = peak[l];
    117.             transform.position = nextPoint;
    118.     yield WaitForSeconds(0.001);
    119.             count = true;
    120.             print(transform.position); 
    121.         }
    122.         if(l == peak.length){nextPoint = endPoint;transform.position = nextPoint;currenPoint = transform.position;}
    123.         if(currenPoint == transform.position){yield WaitForSeconds(0.2);renderer.enabled= false; thunderDone = true;}
    124.         if(x)
    125.         {
    126.         x();   
    127.         }
    128.         }
    129. }
     
  2. nerdboystudios

    nerdboystudios

    Joined:
    Jan 9, 2011
    Posts:
    56
  3. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
  4. beco13

    beco13

    Joined:
    May 31, 2011
    Posts:
    30
    @ivkoni:thx for your link... really inspiring :D
     
    Last edited: Jul 25, 2011