Advertisement
Xmaybeu

Movement System

Apr 15th, 2025
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.80 KB | Source Code | 0 0
  1. local RunService = game:GetService("RunService")
  2. local PlayerService = game:GetService("Players")
  3. local UserInputService = game:GetService("UserInputService")
  4. local TweenService = game:GetService("TweenService")
  5. local SoundService = game:GetService("SoundService")
  6. local DebrisService = game:GetService("Debris")
  7.  
  8. local LocalPlayer = PlayerService.LocalPlayer
  9. local PlayerGui = LocalPlayer.PlayerGui
  10. local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
  11. local Humanoid : Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
  12.  
  13. local Flashlight = workspace:WaitForChild("flashlight")
  14.  
  15. local camera = workspace.CurrentCamera
  16.  
  17. local sin = math.sin
  18. local rad = math.rad
  19. local cos = math.cos
  20. local abs = math.abs
  21. local random = Random.new()
  22.  
  23. local rate = .0075
  24.  
  25. local running = false
  26. local offsetrunning = false
  27.  
  28. local steprate = 20
  29. local cooldown = 0
  30. local stepoffset = 0
  31. local positive = false
  32.  
  33. local flashlightOn = false
  34. local zOffset = 0
  35. local crouchingOffset = 0
  36.  
  37. local idle = false
  38. local proceedingToCrouch = false
  39. local crouching = false
  40.  
  41. local footstepTable : {string} = {
  42.     [Enum.Material.Fabric] = {
  43.         "rbxassetid://6240702531",
  44.         "rbxassetid://133705377"
  45.     },
  46.     [Enum.Material.Rock] = {
  47.         "rbxassetid://90507702118699",
  48.         "rbxassetid://135712042029119",
  49.         "rbxassetid://98172042741214"
  50.     }
  51. }
  52.  
  53. function tweenSprinting(toggle: boolean)
  54.     local info = TweenInfo.new(0.75, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
  55.    
  56.     if Humanoid.MoveDirection.Magnitude > 0 and not idle then
  57.         if toggle then
  58.             if not running then
  59.                 running = true
  60.                 TweenService:Create(Humanoid, info, {WalkSpeed = 13}):Play()
  61.                 TweenService:Create(SoundService.windcamera, info, {Volume = 0.75}):Play()
  62.             end
  63.         else
  64.             if running then
  65.                 running = false
  66.                 TweenService:Create(Humanoid, info, {WalkSpeed = 6}):Play()
  67.                 TweenService:Create(SoundService.windcamera, info, {Volume = 0}):Play()
  68.                 rate = 0.02
  69.             end
  70.         end
  71.     else
  72.         if running then
  73.             running = false
  74.             TweenService:Create(Humanoid, info, {WalkSpeed = 6}):Play()
  75.             TweenService:Create(SoundService.windcamera, info, {Volume = 0}):Play()
  76.             rate = 0.02
  77.         end
  78.     end
  79. end
  80.  
  81. function toggleFlashlight()
  82.     local maxIndex = 15
  83.    
  84.     for i = 1, maxIndex do
  85.         zOffset += 0.005
  86.         task.wait(maxIndex / 500)
  87.     end
  88.    
  89.     local flashlightSound = Instance.new("Sound")
  90.     flashlightSound.Parent = Flashlight
  91.     flashlightSound.Volume = 0.5
  92.     flashlightSound.SoundId = "rbxassetid://78509455631020"
  93.        
  94.     if flashlightOn then
  95.         flashlightSound.PlaybackSpeed = 0.9
  96.         flashlightOn = false
  97.         PlayerGui.MainUI.Timer.Flashlight.Visible = false
  98.         TweenService:Create(Flashlight.SpotLight, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {Brightness = 0}):Play()
  99.         TweenService:Create(Flashlight.Buzzing, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {Volume = 0}):Play()
  100.         TweenService:Create(Flashlight.Spot2, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {Brightness = 0}):Play()
  101.     else
  102.         flashlightSound.PlaybackSpeed = 1
  103.         flashlightOn = true
  104.         PlayerGui.MainUI.Timer.Flashlight.Visible = true
  105.         TweenService:Create(Flashlight.SpotLight, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {Brightness = 2}):Play()
  106.         TweenService:Create(Flashlight.Buzzing, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {Volume = .1}):Play()
  107.         TweenService:Create(Flashlight.Spot2, TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {Brightness = 0.5}):Play()
  108.     end
  109.    
  110.     flashlightSound:Play()
  111.    
  112.     DebrisService:AddItem(flashlightSound, flashlightSound.TimeLength + 0.3)
  113.    
  114.     for i = 1, maxIndex do
  115.         zOffset -= 0.005
  116.         task.wait(maxIndex / 500)
  117.     end
  118. end
  119.  
  120. --[[function toggleCrouching(toggle)
  121.     repeat task.wait() until proceedingToCrouch == false
  122.    
  123.     if crouching == toggle then return end
  124.    
  125.     local maxIndex = 200
  126.     local targetOffset = 2
  127.    
  128.     proceedingToCrouch = true
  129.    
  130.     print("being called")
  131.    
  132.     if toggle then
  133.         for i = 1, maxIndex do
  134.             crouchingOffset += targetOffset / maxIndex
  135.             task.wait(targetOffset / (maxIndex * 5))
  136.         end
  137.         crouchingOffset = targetOffset
  138.        
  139.         crouching = true
  140.     else
  141.         for i = 1, maxIndex do
  142.             crouchingOffset -= targetOffset / maxIndex
  143.             task.wait(targetOffset / (maxIndex * 5))
  144.         end
  145.  
  146.         crouchingOffset = 0
  147.        
  148.         crouching = false
  149.     end
  150.    
  151.     proceedingToCrouch = false
  152. end]]
  153.  
  154. function sendFootstep()
  155.     local floorMaterial = footstepTable[Humanoid.FloorMaterial]
  156.    
  157.     if floorMaterial then
  158.         local id = floorMaterial[math.random(1, #floorMaterial)]
  159.        
  160.         local newSound = Instance.new("Sound")
  161.         newSound.Parent = SoundService
  162.         newSound.Volume = 0.45
  163.         newSound.SoundId = id
  164.        
  165.         newSound.PlaybackSpeed = Random.new():NextNumber(.2, .4)
  166.         if running then
  167.             newSound.PlaybackSpeed = Random.new():NextNumber(.4, .6)
  168.             newSound.Volume = .65
  169.         end
  170.         newSound:Play()
  171.         DebrisService:AddItem(newSound, 3)
  172.     end
  173. end
  174.  
  175. function updateCamera(deltaTime: number)
  176.     Flashlight.CFrame = camera.CFrame
  177.    
  178.     if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) then
  179.         if Humanoid.MoveDirection.Magnitude > 0 and not crouching then
  180.             tweenSprinting(true)
  181.         else
  182.             tweenSprinting(false)
  183.         end
  184.     else
  185.         tweenSprinting(false)
  186.         if rate > 0.0075 then
  187.             rate -= 0.0002
  188.         end
  189.     end
  190.    
  191.     if UserInputService:IsKeyDown(Enum.KeyCode.LeftControl) then
  192.         if not running then
  193.             --task.spawn(toggleCrouching, true)
  194.         end
  195.     else
  196.         --task.spawn(toggleCrouching, false)
  197.     end
  198.    
  199.     if Humanoid.MoveDirection.Magnitude > 0 then
  200.         idle = false
  201.        
  202.         cooldown += deltaTime
  203.  
  204.         if stepoffset > 0 then
  205.             stepoffset -= 0.002
  206.         else
  207.             stepoffset = 0
  208.         end
  209.  
  210.         if cooldown >= steprate then
  211.             cooldown = 0
  212.             stepoffset = .02
  213.            
  214.             sendFootstep()
  215.         end
  216.     else
  217.         idle = true
  218.     end
  219.    
  220.     if Humanoid.MoveDirection.Magnitude == 0 then
  221.         local bobbleX = rad(cos(tick() * 3) * .025)
  222.         local bobbleY = rad(abs(sin(tick()) * 1.4 / 2.3)) * .035
  223.         local bobbleZ = rad(cos(tick() * 3.5 / 1.5) * .04) + zOffset
  224.  
  225.         local newCFrame = camera.CFrame * CFrame.Angles(bobbleX, bobbleY, bobbleZ)
  226.  
  227.         camera.CFrame = newCFrame
  228.     elseif running then
  229.         steprate = 0.26
  230.        
  231.         local bobbleX = rad(cos((tick()) * 13 / 1.4) * .2)
  232.         local bobbleY = rad(sin((tick()) * 1.4 / 2.3) * .1)
  233.         local bobbleZ = (rad(cos((tick()) * 7 / 1.4) * 0.7) + (random:NextNumber(-rate* 1.05, rate* 1.05))) + zOffset
  234.  
  235.         if math.random(1, 2) == 1 then
  236.             if not positive then
  237.                 positive = true
  238.                 bobbleX += (random:NextNumber(-rate * 2.1, rate * 2.1)) - (stepoffset * 1.75)
  239.             else
  240.                 positive = false
  241.                 bobbleX += (random:NextNumber(-rate * 2, rate * 2)) + (stepoffset * 1.75)
  242.             end
  243.            
  244.             bobbleY += (random:NextNumber(-rate *1.6, rate*1.6))
  245.         end
  246.        
  247.         local newCFrame = camera.CFrame * CFrame.Angles(bobbleX, bobbleY, bobbleZ)
  248.         camera.CFrame = newCFrame
  249.     elseif proceedingToCrouch then
  250.         local bobbleX = rad(cos((tick()) * 10 / 1.5) * .02)
  251.         local bobbleY = rad(sin((tick()) * 1.4 / 2.3) * .01)
  252.         local bobbleZ = rad(cos((tick()) * 6 / 1.5) * 0.15) + zOffset
  253.  
  254.         if math.random(1, 3) == 1 then
  255.             if not positive then
  256.                 positive = true
  257.                 bobbleX += (random:NextNumber(-rate* 1.2, rate * 1.2))
  258.             else
  259.                 positive = false
  260.                 bobbleX += (random:NextNumber(-rate, rate))
  261.             end
  262.  
  263.             bobbleY += (random:NextNumber(-rate/1.5, rate/1.5))
  264.         end
  265.  
  266.         local newCFrame = camera.CFrame * CFrame.Angles(bobbleX, bobbleY, bobbleZ)
  267.        
  268.        
  269.     else
  270.         steprate = 1.16
  271.  
  272.         local bobbleX = rad(cos((tick()) * 10 / 1.5) * .02)
  273.         local bobbleY = rad(sin((tick()) * 1.4 / 2.3) * .01)
  274.         local bobbleZ = rad(cos((tick()) * 6 / 1.5) * 0.15) + zOffset
  275.  
  276.         if math.random(1, 3) == 1 then
  277.             if not positive then
  278.                 positive = true
  279.                 bobbleX += (random:NextNumber(-rate/1.1, rate/1.1)) - stepoffset
  280.             else
  281.                 positive = false
  282.                 bobbleX += (random:NextNumber(-rate/1.1, rate/1.1)) + stepoffset
  283.             end
  284.  
  285.             bobbleY += (random:NextNumber(-rate/1.8, rate/1.8))
  286.         end
  287.  
  288.         local newCFrame = camera.CFrame * CFrame.Angles(bobbleX, bobbleY, bobbleZ)
  289.  
  290.         camera.CFrame = newCFrame
  291.     end
  292.    
  293.     local divide = 7
  294.     local offset = Vector3.new(random:NextNumber(-rate / divide, rate / divide), random:NextNumber(-rate / divide, rate / divide), 0)
  295.  
  296.     Humanoid.CameraOffset = Vector3.new(0, 3 - crouchingOffset, 0)
  297.  
  298.     --[[if math.random(1, 10) == 1 and not offsetrunning then
  299.         offsetrunning = true
  300.         for i = 0, .25, .025 do
  301.             Humanoid.CameraOffset = Humanoid.CameraOffset:Lerp(offset, .40) + Vector3.new(0, 1.25 - crouchingOffset, 0)
  302.             task.wait(.01)
  303.         end
  304.         offsetrunning = false
  305.     end]]
  306. end
  307.  
  308. UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
  309.     if gameProcessedEvent then return end
  310.  
  311.     if input.KeyCode == Enum.KeyCode.F then
  312.         if not running then
  313.             toggleFlashlight()
  314.         end
  315.     end
  316. end)
  317.  
  318. RunService.RenderStepped:Connect(updateCamera)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement