Advertisement
Guest User

footplant

a guest
Feb 9th, 2020
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. local runService = game:GetService("RunService")
  2.  
  3. local player = game:GetService("Players").LocalPlayer
  4. local character = player.Character
  5. local rootPart = character.HumanoidRootPart
  6. local camera = workspace.CurrentCamera
  7.  
  8. local beam1, beam2, pos1, pos2 = workspace.beam1, workspace.beam2, workspace.pos1, workspace.pos2
  9.  
  10. local cf = CFrame.new
  11. local ang = CFrame.Angles
  12. local v3 = Vector3.new
  13. local ray = Ray.new
  14. local rad = math.rad
  15. local deg = math.deg
  16.  
  17. local legToUpdate = true --true==left//false==right
  18. local lastUpdate = 0
  19. local alpha = 0
  20. local direction = v3()
  21. local leftPos = v3()
  22. local rightPos = v3()
  23. local targLeft = v3()
  24. local targRight = v3()
  25. local leftLift = 0
  26. local rightLift = 0
  27.  
  28. local walkAlpha = 2
  29.  
  30. local function lerp(n1, n2, perc)
  31.     return n2 + (n1 - n2) * perc
  32. end
  33.  
  34. local function getVel()
  35.     local vel = rootPart.CFrame:vectorToObjectSpace(rootPart.Velocity)
  36.     local speed = v3(vel.X, 0, vel.Z).magnitude
  37.     return vel, speed
  38. end
  39.  
  40. local function getDirection()
  41.     local direction = camera.CFrame.lookVector
  42.     local heading = math.atan2(-direction.X, -direction.Z)
  43.     return heading
  44. end
  45.  
  46. local function updateLegs(leftOrigin, rightOrigin, leftPos, rightPos)
  47.     local d1 = (leftOrigin.p - leftPos).magnitude
  48.     beam1.Size = v3(0.5, 0.5, d1)
  49.     beam1.CFrame = cf(leftOrigin.p, leftPos) * cf(0, 0, -d1/2)
  50.     local d2 = (rightOrigin.p - rightPos).magnitude
  51.     beam2.Size = v3(0.5, 0.5, d2)
  52.     beam2.CFrame = cf(rightOrigin.p, rightPos) * cf(0, 0, -d2/2)
  53. end
  54.  
  55. local function getNextPosition(origin, xAngle, zAngle, length, lift)
  56.     local start = origin * ang(0, 0, zAngle) * ang(xAngle, 0, 0)
  57.     local ray = ray(start.p, -start.UpVector * length)
  58.     local hit, pos, nor = workspace:FindPartOnRayWithIgnoreList(ray, {character, beam1, beam2, workspace.Ignore})
  59.     return pos + v3(0, lift, 0)
  60. end
  61.  
  62. game:GetService("RunService").RenderStepped:Connect(function(dt)
  63.     local vel, speed = getVel()
  64.     rootPart.CFrame = cf(rootPart.CFrame.p) * ang(0, getDirection(), 0)
  65.     local leftOrigin, rightOrigin =character.Head.CFrame  * cf(-0.5, -0.5, 0), character.Head.CFrame * cf(0.5, -0.5, 0)
  66.     local hipHeight = 3
  67.     if speed > 0 then --if we are moving
  68.         direction = direction:lerp(vel.Unit, 10*dt)
  69.         if alpha >= 1 then--lastUpdate + delayOf < tick() then
  70.             lastUpdate = tick()
  71.             legToUpdate = not legToUpdate
  72.             alpha = 0
  73.         end
  74.         alpha = alpha + dt*walkAlpha--(tick() - lastUpdate)/(1/character.Humanoid.WalkSpeed)
  75.         local lift = 0.5*math.sin(math.pi * alpha)
  76.         local fba = direction.Z * -16
  77.         local lra = direction.X * 16
  78.         if legToUpdate then
  79.             leftPos = getNextPosition(leftOrigin, rad(fba), rad(lra), hipHeight, lift)
  80.         else
  81.             rightPos = getNextPosition(rightOrigin, rad(fba), rad(lra), hipHeight, lift)
  82.         end
  83.     else
  84.         direction = direction:lerp((rootPart.CFrame.lookVector * v3(1, 0, 1)).unit, 10*dt)
  85.         leftPos = getNextPosition(leftOrigin, rad(10), rad(2), hipHeight, 0)
  86.         rightPos = getNextPosition(rightOrigin, rad(-10), rad(-2), hipHeight, 0)
  87.     end
  88.     targLeft = targLeft:lerp(leftPos, math.min(1, dt*30))
  89.     targRight = targRight:lerp(rightPos, math.min(1, dt*30))
  90.     updateLegs(leftOrigin, rightOrigin, targLeft, targRight)
  91. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement