Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Sprint Script
- Features:
- • Smooth acceleration/deceleration for walk ↔ sprint transitions
- • Stamina management with depletion and regeneration
- • Subtle camera roll (tilt) on sprint using local forward‐axis rotation
- • Configurable parameters for speed, rates, and tilt
- ]]
- local Players = game:GetService("Players")
- local RunService = game:GetService("RunService")
- local UserInputService = game:GetService("UserInputService")
- local player = Players.LocalPlayer
- local camera = workspace.CurrentCamera
- local character, humanoid, rootPart
- -- Configuration
- local walkSpeed = 16
- local sprintSpeed = 32
- local accelRate = 50 -- studs/s²
- local decelRate = 70 -- studs/s²
- local maxStamina = 5 -- seconds
- local staminaDrain = 1 -- per second
- local staminaRegen = 0.5 -- per second
- local tiltAngle = 5 -- degrees of roll
- local tiltSpeed = 10 -- interpolation speed
- -- State
- local currentSpeed = walkSpeed
- local isSprinting = false
- local stamina = maxStamina
- local currentRoll = 0 -- radians
- -- Handle respawns
- local function onCharacterAdded(char)
- character = char
- humanoid = char:WaitForChild("Humanoid")
- rootPart = char:WaitForChild("HumanoidRootPart")
- -- reset state
- currentSpeed = walkSpeed
- isSprinting = false
- stamina = maxStamina
- currentRoll = 0
- end
- player.CharacterAdded:Connect(onCharacterAdded)
- if player.Character then
- onCharacterAdded(player.Character)
- end
- -- Detect sprint input
- UserInputService.InputBegan:Connect(function(input, processed)
- if not processed and input.KeyCode == Enum.KeyCode.LeftShift then
- isSprinting = true
- end
- end)
- UserInputService.InputEnded:Connect(function(input, processed)
- if not processed and input.KeyCode == Enum.KeyCode.LeftShift then
- isSprinting = false
- end
- end)
- -- Main update loop
- RunService.RenderStepped:Connect(function(dt)
- if not humanoid then return end
- local moving = humanoid.MoveDirection.Magnitude > 0
- -- Determine target speed
- local targetSpeed = (isSprinting and moving and stamina > 0)
- and sprintSpeed
- or walkSpeed
- -- Smooth acceleration/deceleration
- if currentSpeed < targetSpeed then
- currentSpeed = math.min(currentSpeed + accelRate * dt, targetSpeed)
- elseif currentSpeed > targetSpeed then
- currentSpeed = math.max(currentSpeed - decelRate * dt, targetSpeed)
- end
- humanoid.WalkSpeed = currentSpeed
- -- Stamina logic
- if isSprinting and moving and stamina > 0 then
- stamina = math.max(stamina - staminaDrain * dt, 0)
- if stamina == 0 then
- isSprinting = false
- end
- else
- stamina = math.min(stamina + staminaRegen * dt, maxStamina)
- end
- -- Improved camera roll tilt
- local targetRoll = (currentSpeed > walkSpeed + 0.1)
- and math.rad(tiltAngle)
- or 0
- local alpha = math.clamp(tiltSpeed * dt, 0, 1)
- local deltaRoll = (targetRoll - currentRoll) * alpha
- if math.abs(deltaRoll) > 1e-4 then
- -- Rotate camera around its local forward axis
- camera.CFrame = camera.CFrame * CFrame.Angles(0, 0, deltaRoll)
- currentRoll = currentRoll + deltaRoll
- end
- end)
Advertisement
Advertisement