Advertisement
jordan83221

Tail

Mar 25th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.14 KB | None | 0 0
  1. local next=next
  2. local gravity=0.09
  3. local friction=0.7
  4. local bounce=0.8
  5. local player=game.Players.LocalPlayer
  6. local torso=player.Character.Torso
  7. local function dist(p0,p1)
  8.     local dx=p1.CFrame.x-p0.CFrame.x
  9.     local dy=p1.CFrame.y-p0.CFrame.y
  10.     local dz=p1.CFrame.z-p0.CFrame.z
  11.     return math.sqrt(dx*dx+dy*dy+dz*dz)
  12. end
  13. local points={}
  14. local joints={}
  15. local function createPoint(x,y,z,pinned)
  16.     local t = {x=x,y=y,z=z,oldx=x-0.4,oldy=y-0.4,oldz=z-0.4,pinned=pinned}
  17.     points[#points+1]=t
  18.     return t
  19. end
  20. local function createJoint(point1,point2,distance,line)
  21.     local t = {point1=point1,point2=point2,distance=distance,line=line}
  22.     table.insert(joints,t)
  23. end
  24. local function drawLine()
  25.     for i=1,#joints do
  26.         local j=joints[i]
  27.         local line=j.line
  28.         if not line then
  29.             line=Instance.new("Part",player.Character)
  30.             line.BrickColor=player.Character.Torso.BrickColor
  31.             line.Anchored=true
  32.             line.CanCollide=false
  33.             line.BottomSurface="Smooth"
  34.             line.TopSurface="Smooth"
  35.             local mesh=Instance.new("BlockMesh",line)
  36.             mesh.Scale=Vector3.new(1,1,1)
  37.             j.line=line
  38.         end
  39.         local dx=j.point2.x-j.point1.x
  40.         local dy=j.point2.y-j.point1.y
  41.         local dz=j.point2.z-j.point1.z
  42.         local distance=math.sqrt(dx*dx+dy*dy+dz*dz)
  43.         line.Size=Vector3.new(0.1,0.4,distance)
  44.         line.CFrame=CFrame.new(Vector3.new(j.point1.x,j.point1.y,j.point1.z),Vector3.new(j.point2.x,j.point2.y,j.point2.z))
  45.         *CFrame.new(0,0,-distance/2)
  46.     end
  47. end
  48. local width=1
  49. local height=15
  50. local l=0.25
  51. for y=1,height do
  52.     for x=1,width do
  53.         createPoint(l*x,25-l*y,0,y==1)
  54.         if x>1 then
  55.             createJoint(points[width*(y-1)+x-1],points[width*(y-1)+x],l)
  56.         end
  57.         if y>1 then
  58.             createJoint(points[width*(y-2)+x],points[width*(y-1)+x],l)
  59.         end
  60.     end
  61. end
  62. local function updatePoints()
  63.     for i=1,#points do
  64.         local p=points[i]
  65.         if not p.pinned then
  66.             local vx=(p.x-p.oldx)*friction;
  67.             local vy=-((p.y-p.oldy)*friction);
  68.             local vz=(p.z-p.oldz)*friction;
  69.             p.oldx=p.x
  70.             p.oldy=p.y
  71.             p.oldz=p.z
  72.             p.x=p.x+vx
  73.             p.y=p.y-vy
  74.             p.z=p.z+vz
  75.             p.y=p.y-gravity
  76.             if p.y<1 then
  77.                 p.y=1
  78.                 p.oldy=p.y-vy*bounce;
  79.             end
  80.         end
  81.     end
  82. end
  83. local function updateSticks()
  84.     for i=1,#joints do
  85.         local j=joints[i]
  86.         local dx=j.point2.x-j.point1.x
  87.         local dy=j.point2.y-j.point1.y
  88.         local dz=j.point2.z-j.point1.z
  89.         local distance=math.sqrt(dx*dx+dy*dy+dz*dz)
  90.         local difference=j.distance-distance
  91.         local percent=difference/distance/2
  92.         local offsetX=dx*percent;
  93.         local offsetY=dy*percent;
  94.         local offsetZ=dz*percent;
  95.         if j.point1.pinned or j.point2.pinned then
  96.             offsetX=offsetX*2
  97.             offsetY=offsetY*2
  98.             offsetZ=offsetZ*2
  99.         end
  100.         if not j.point1.pinned then
  101.             j.point1.x=j.point1.x-offsetX
  102.             j.point1.y=j.point1.y-offsetY
  103.             j.point1.z=j.point1.z-offsetZ
  104.         end
  105.         if not j.point2.pinned then
  106.             j.point2.x=j.point2.x+offsetX
  107.             j.point2.y=j.point2.y+offsetY
  108.             j.point2.z=j.point2.z+offsetZ
  109.         end
  110.     end
  111. end
  112. game:service'RunService'.RenderStepped:connect(function()
  113.     for i=1,width do
  114.         local p=torso.CFrame*CFrame.new(0,-0.75,0.45)
  115.         points[i].x=p.x
  116.         points[i].y=p.y
  117.         points[i].z=p.z
  118.     end
  119.     updatePoints()
  120.     for i=1,20 do
  121.         updateSticks()
  122.     end
  123.     drawLine()
  124. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement