Advertisement
ailenkai

Untitled

Nov 13th, 2024 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.06 KB | None | 0 0
  1. -- ailenkai
  2.  
  3. local keyframeSequenceProvider = game:GetService("KeyframeSequenceProvider")
  4. local playersService = game:GetService("Players")
  5. local userInputService = game:GetService("UserInputService")
  6. local replicatedStorage = game:GetService("ReplicatedStorage")
  7. local runService = game:GetService("RunService")
  8.  
  9. local player = playersService.LocalPlayer
  10. local character = player.Character or player.CharacterAdded:Wait()
  11. local humanoid:Humanoid = character:WaitForChild("Humanoid")
  12. local root = character:WaitForChild("HumanoidRootPart")
  13. local camera = workspace.CurrentCamera
  14. local ball = character:WaitForChild("ball")
  15. local updateEvent = replicatedStorage:WaitForChild("update")
  16.  
  17. camera.CameraType = Enum.CameraType.Custom
  18. humanoid.WalkSpeed = 50 -- Sets move speed
  19.  
  20. local function lerp(from, to, alpha)
  21.     return from + (to - from) * alpha
  22. end
  23.  
  24. local currentMoveDirections = { W = false, A = false, S = false, D = false, Space = false }
  25.  
  26. local function onInputBegan(input, gameProcessedEvent)
  27.     if not gameProcessedEvent then
  28.         local key = input.KeyCode.Name
  29.         if currentMoveDirections[key] ~= nil then
  30.             currentMoveDirections[key] = true
  31.         end
  32.     end
  33. end
  34.  
  35. local function onInputEnded(input, gameProcessedEvent)
  36.     if not gameProcessedEvent then
  37.         local key = input.KeyCode.Name
  38.         if currentMoveDirections[key] ~= nil then
  39.             currentMoveDirections[key] = false
  40.         end
  41.     end
  42. end
  43.  
  44. task.wait(1)
  45.  
  46. local function createPreviewAnimation(keyframeSequence)
  47.     local hashId = keyframeSequenceProvider:RegisterKeyframeSequence(keyframeSequence)
  48.     local Animation = Instance.new("Animation")
  49.     Animation.AnimationId = hashId
  50.     return Animation
  51. end
  52.  
  53. local animator = humanoid:FindFirstChildOfClass("Animator")
  54. local animation = ball:FindFirstChild(humanoid.RigType == Enum.HumanoidRigType.R15 and "R15ballRun" or "R6ballRun")
  55. local tempAnim = createPreviewAnimation(animation:FindFirstChildOfClass("KeyframeSequence"))
  56. local animationTrack = animator and animator:LoadAnimation(tempAnim)
  57.  
  58. if animationTrack then
  59.     animationTrack:Play()
  60. end
  61.  
  62. local hasLandedRay = nil
  63. local isFalling = false
  64.  
  65. local params = RaycastParams.new()
  66. params.FilterDescendantsInstances = {ball, character}
  67. params.FilterType = Enum.RaycastFilterType.Exclude
  68.  
  69. local function onRenderStepped(deltaTime)
  70.     if character and humanoid.Health > 0 then
  71.         local speed = ball.AssemblyLinearVelocity.Magnitude
  72.         local lookVectorX, lookVectorZ = camera.CFrame.LookVector.X, camera.CFrame.LookVector.Z
  73.         local lookVector = Vector3.new(lookVectorX, 0, lookVectorZ).Unit
  74.         local rightVector = lookVector:Cross(Vector3.new(0, 1, 0))
  75.  
  76.         local function calculateTargetMoveDirection()
  77.             local moveDirection = Vector3.zero
  78.  
  79.             if currentMoveDirections.W then
  80.                 moveDirection = moveDirection + lookVector
  81.             end
  82.             if currentMoveDirections.S then
  83.                 moveDirection = moveDirection - lookVector
  84.             end
  85.             if currentMoveDirections.D then
  86.                 moveDirection = moveDirection + rightVector
  87.             end
  88.             if currentMoveDirections.A then
  89.                 moveDirection = moveDirection - rightVector
  90.             end
  91.  
  92.             if moveDirection.Magnitude > 0 then
  93.                 moveDirection = moveDirection.Unit
  94.             end
  95.  
  96.             return moveDirection
  97.         end
  98.  
  99.         local targetMoveDirection = calculateTargetMoveDirection()
  100.  
  101.         if targetMoveDirection.Magnitude > 0 then
  102.             ball.AssemblyLinearVelocity += targetMoveDirection * humanoid.WalkSpeed * deltaTime
  103.         end
  104.        
  105.         hasLandedRay = workspace:Raycast(ball.Position, Vector3.new(0,-(ball.Size.Y + 0.1), 0), params)
  106.         if hasLandedRay then
  107.             isFalling = false
  108.         else
  109.             isFalling = true
  110.         end
  111.        
  112.         if currentMoveDirections.Space and not isFalling then
  113.             ball.AssemblyLinearVelocity += Vector3.new(0,5,0)
  114.         end
  115.  
  116.         updateEvent:FireServer((humanoid.MoveDirection.Magnitude > 0.1) and humanoid.MoveDirection or root.AssemblyLinearVelocity)
  117.  
  118.         if animationTrack then
  119.             animationTrack:AdjustSpeed(root.AssemblyLinearVelocity.Magnitude / humanoid.WalkSpeed)
  120.         end
  121.     end
  122. end
  123.  
  124. userInputService.InputBegan:Connect(onInputBegan)
  125. userInputService.InputEnded:Connect(onInputEnded)
  126. runService.RenderStepped:Connect(onRenderStepped)
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement