Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local runService = game:GetService("RunService")
- local player = game:GetService("Players").LocalPlayer
- local character = player.Character
- local rootPart = character.HumanoidRootPart
- local camera = workspace.CurrentCamera
- local beam1, beam2, pos1, pos2 = workspace.beam1, workspace.beam2, workspace.pos1, workspace.pos2
- local cf = CFrame.new
- local ang = CFrame.Angles
- local v3 = Vector3.new
- local ray = Ray.new
- local rad = math.rad
- local deg = math.deg
- local legToUpdate = true --true==left//false==right
- local lastUpdate = 0
- local alpha = 0
- local direction = v3()
- local leftPos = v3()
- local rightPos = v3()
- local targLeft = v3()
- local targRight = v3()
- local leftLift = 0
- local rightLift = 0
- local walkAlpha = 2
- local function lerp(n1, n2, perc)
- return n2 + (n1 - n2) * perc
- end
- local function getVel()
- local vel = rootPart.CFrame:vectorToObjectSpace(rootPart.Velocity)
- local speed = v3(vel.X, 0, vel.Z).magnitude
- return vel, speed
- end
- local function getDirection()
- local direction = camera.CFrame.lookVector
- local heading = math.atan2(-direction.X, -direction.Z)
- return heading
- end
- local function updateLegs(leftOrigin, rightOrigin, leftPos, rightPos)
- local d1 = (leftOrigin.p - leftPos).magnitude
- beam1.Size = v3(0.5, 0.5, d1)
- beam1.CFrame = cf(leftOrigin.p, leftPos) * cf(0, 0, -d1/2)
- local d2 = (rightOrigin.p - rightPos).magnitude
- beam2.Size = v3(0.5, 0.5, d2)
- beam2.CFrame = cf(rightOrigin.p, rightPos) * cf(0, 0, -d2/2)
- end
- local function getNextPosition(origin, xAngle, zAngle, length, lift)
- local start = origin * ang(0, 0, zAngle) * ang(xAngle, 0, 0)
- local ray = ray(start.p, -start.UpVector * length)
- local hit, pos, nor = workspace:FindPartOnRayWithIgnoreList(ray, {character, beam1, beam2, workspace.Ignore})
- return pos + v3(0, lift, 0)
- end
- game:GetService("RunService").RenderStepped:Connect(function(dt)
- local vel, speed = getVel()
- rootPart.CFrame = cf(rootPart.CFrame.p) * ang(0, getDirection(), 0)
- local leftOrigin, rightOrigin =character.Head.CFrame * cf(-0.5, -0.5, 0), character.Head.CFrame * cf(0.5, -0.5, 0)
- local hipHeight = 3
- if speed > 0 then --if we are moving
- direction = direction:lerp(vel.Unit, 10*dt)
- if alpha >= 1 then--lastUpdate + delayOf < tick() then
- lastUpdate = tick()
- legToUpdate = not legToUpdate
- alpha = 0
- end
- alpha = alpha + dt*walkAlpha--(tick() - lastUpdate)/(1/character.Humanoid.WalkSpeed)
- local lift = 0.5*math.sin(math.pi * alpha)
- local fba = direction.Z * -16
- local lra = direction.X * 16
- if legToUpdate then
- leftPos = getNextPosition(leftOrigin, rad(fba), rad(lra), hipHeight, lift)
- else
- rightPos = getNextPosition(rightOrigin, rad(fba), rad(lra), hipHeight, lift)
- end
- else
- direction = direction:lerp((rootPart.CFrame.lookVector * v3(1, 0, 1)).unit, 10*dt)
- leftPos = getNextPosition(leftOrigin, rad(10), rad(2), hipHeight, 0)
- rightPos = getNextPosition(rightOrigin, rad(-10), rad(-2), hipHeight, 0)
- end
- targLeft = targLeft:lerp(leftPos, math.min(1, dt*30))
- targRight = targRight:lerp(rightPos, math.min(1, dt*30))
- updateLegs(leftOrigin, rightOrigin, targLeft, targRight)
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement