GoldN

LeanScript

Dec 4th, 2020
870
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local RunService = game:GetService("RunService")
  2.  
  3. local UP = Vector3.new(0, 1, 0)
  4. local ZERO = Vector3.new()
  5.  
  6. local character = script.Parent
  7.     local root = character:WaitForChild("HumanoidRootPart")
  8.     local torso = character:WaitForChild("Torso")
  9.     local rootJoint = root:WaitForChild("RootJoint")
  10.     local leftHip = torso:WaitForChild("Left Hip")
  11.     local rightHip = torso:WaitForChild("Right Hip")
  12.     local humanoid = character:WaitForChild("Humanoid")
  13.  
  14. local LEAN_ANGLE = 0.3
  15. local LERP_STRENGTH = 0.2
  16.  
  17. local cache = {
  18.     root = {
  19.         C0 = rootJoint.C0 - rootJoint.C0.Position,
  20.         C1 = rootJoint.C1 - rootJoint.C1.Position
  21.     },
  22.     leftHip = {
  23.         C0 = leftHip.C0 - leftHip.C0.Position,
  24.         C1 = leftHip.C1 - leftHip.C1.Position
  25.     },
  26.     rightHip = {
  27.         C0 = rightHip.C0 - rightHip.C0.Position,
  28.         C1 = rightHip.C1 - rightHip.C1.Position
  29.     }
  30. }
  31.  
  32. local rootOffset = CFrame.new()
  33. local leftHipOffset = CFrame.new()
  34. local rightHipOffset = CFrame.new()
  35. RunService.Stepped:Connect(function()
  36.     local newDirection = humanoid.MoveDirection
  37.    
  38.     local newRootOffset = CFrame.new()
  39.     local newLeftHipOffset = CFrame.new()
  40.     local newRightHipOffset = CFrame.new()
  41.     if newDirection ~= ZERO then
  42.         local relativeDirection = (root.CFrame - root.Position):Inverse() * newDirection
  43.  
  44.         local axis = relativeDirection:Cross(UP)
  45.         newRootOffset = cache.root.C1:Inverse()
  46.             * CFrame.fromAxisAngle(axis, -LEAN_ANGLE)
  47.             * cache.root.C0
  48.         newLeftHipOffset = cache.leftHip.C1:Inverse()
  49.             * CFrame.fromAxisAngle(axis, LEAN_ANGLE)
  50.             * cache.leftHip.C0
  51.         newRightHipOffset = cache.rightHip.C1:Inverse()
  52.             * CFrame.fromAxisAngle(axis, LEAN_ANGLE)
  53.             * cache.rightHip.C0
  54.     end
  55.    
  56.     rootOffset = rootOffset:Lerp(newRootOffset, LERP_STRENGTH)
  57.     leftHipOffset = leftHipOffset:Lerp(newLeftHipOffset, LERP_STRENGTH)
  58.     rightHipOffset = rightHipOffset:Lerp(newRightHipOffset, LERP_STRENGTH)
  59.    
  60.     rootJoint.Transform *= rootOffset
  61.     leftHip.Transform *= leftHipOffset
  62.     rightHip.Transform *= rightHipOffset
  63. end)
RAW Paste Data