Advertisement
CloneTrooper1019

makeLightningBolt

Mar 10th, 2015
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.72 KB | None | 0 0
  1. function makeLightningBolt(a,b)
  2.     local dist = (b.p-a.p).magnitude
  3.     local segments = math.random(math.floor(dist*0.2),math.floor(dist*0.5))
  4.     local boltMdl = Instance.new("Model")
  5.     boltMdl.Name = "LightningBolt"
  6.     local points = {}
  7.     local function slerp(c0,c1,a)
  8.         local c0 = CFrame.new(c0.p) * CFrame.Angles(-math.pi/2,0,0)
  9.         local c1 = CFrame.new(c1.p)
  10.         local startDir = c0.lookVector
  11.         local flightDir = (c1.p-c0.p)
  12.         local totalDist = flightDir.magnitude
  13.         local aheadVec = startDir.unit*totalDist
  14.         return CFrame.new(c0.p + (aheadVec*a)*(1-a)+(flightDir*a)*a)
  15.     end
  16.     local function placeSegment(at)
  17.         local segment = Instance.new("Part",boltMdl)
  18.         segment.Anchored = true
  19.         segment.FormFactor = "Custom"
  20.         segment.Shape = "Ball"
  21.         segment.TopSurface = "Smooth"
  22.         segment.BottomSurface = "Smooth"
  23.         segment.Material = "SmoothPlastic"
  24.         segment.BrickColor = BrickColor.new("New Yeller")
  25.         segment.CFrame = CFrame.new(at.p)
  26.         segment.Size = Vector3.new(2,2,2)
  27.         table.insert(points,segment)
  28.     end
  29.     placeSegment(a)
  30.     for i = 1/segments,1,1/segments do
  31.         local pos = slerp(a,b,i) + Vector3.new(math.random(-5,5),math.random(0,10),math.random(-5,5))
  32.         placeSegment(pos)
  33.     end
  34.     placeSegment(b)
  35.     table.sort(points,function (v1,v2)
  36.         return v1.Position.Y < v2.Position.Y
  37.     end)
  38.     local p
  39.     for _,s in pairs(points) do
  40.         local connector
  41.         if p then
  42.             local off = (s.Position-p.Position)
  43.             connector = p:clone()
  44.             connector.Shape = "Block"
  45.             connector.Size = Vector3.new(2,off.magnitude,2)
  46.             connector.CFrame = CFrame.new(p.Position+(off/2),s.Position) * CFrame.Angles(-math.pi/2,0,0)
  47.             connector.Parent = boltMdl
  48.             Instance.new("CylinderMesh",connector)
  49.         end
  50.         p = s
  51.     end
  52.     boltMdl.Parent = workspace
  53.     return boltMdl
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement