I’ve created a simple sprinting and sliding mechanic that can be easily incorporated into your game.
Features:
- Press Shift to Sprint
- Press C to Slide
- Sliding gradually slows down over time
- Sliding halts when the player encounters a wall in front.
How to Use:
- Copy the code below.
- Insert this code into a local script.
- Place the Local Script in Game > StarterPlayer > StarterCharacterScripts.
- Enjoy!
How to Create an Animation:
- First, create a default dummy.
- Next, open the animator and create a basic sliding idle position. (Use only one keyframe for this.)
- In the animator, click the three dots and select “Set Animation Priority”, then set it to “Movement”.
- In the animator, click the three dots and select “Import”.
- Replace the old id in the code with the new id from the import.
I am lazy so I used chatgpt for the comments
Code:
-- Initialization of constants and services
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- Player related constants
local PLAYER = Players.LocalPlayer
local CHARACTER = PLAYER.Character or PLAYER.CharacterAdded:Wait()
local HUMANOID = CHARACTER:WaitForChild("Humanoid")
local ROOT_PART = CHARACTER:WaitForChild("HumanoidRootPart")
-- Reset camera offset
HUMANOID.CameraOffset = Vector3.new(0, 0, 0)
-- Animation setup
local ANIMATION_ID = "rbxassetid://14138121674" -- replace with your animation id
local animation = Instance.new("Animation")
animation.AnimationId = ANIMATION_ID
local animationTrack = HUMANOID:LoadAnimation(animation)
-- Sliding related variables
local isSliding = false
local slideStartTime = 0
local SLIDE_DURATION = 1 -- Slide for at least 1 seconds
local slideMultiplier = .6
-- Speed related constants
local SPRINT_SPEED = 25 -- Change to your preferred sprint speed
local WALK_SPEED = 16 -- Change to your preferred walk speed
-- Function to stop slide
local function stopSlide()
if isSliding then
isSliding = false
if ROOT_PART:FindFirstChild("BodyVelocity") then
ROOT_PART.BodyVelocity:Destroy()
end
HUMANOID.CameraOffset = Vector3.new(0, 0, 0)
animationTrack:Stop() -- stop the animation
end
end
-- Function to start slide
local function startSlide()
if HUMANOID:GetState() ~= Enum.HumanoidStateType.Freefall and ROOT_PART.Velocity.Magnitude > 5 then
if isSliding then
stopSlide()
else
isSliding = true
slideStartTime = tick()
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = Vector3.new(ROOT_PART.Velocity.X*2, 0, ROOT_PART.Velocity.Z*2)
bodyVelocity.MaxForce = Vector3.new(math.huge, 0, math.huge)
bodyVelocity.Parent = ROOT_PART
HUMANOID.CameraOffset = Vector3.new(0, -2, 0)
animationTrack:Play() -- play the animation
end
end
end
-- Listen to key inputs and manage character actions
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.C then
startSlide()
elseif input.KeyCode == Enum.KeyCode.Space then
stopSlide()
elseif input.KeyCode == Enum.KeyCode.LeftShift then
HUMANOID.WalkSpeed = SPRINT_SPEED
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
HUMANOID.WalkSpeed = WALK_SPEED
end
end)
-- Set up raycast parameters
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {CHARACTER}
local RunService = game:GetService("RunService")
game:GetService("RunService").Heartbeat:Connect(function()
-- Define the directions for the raycasts
local directions = {
left = CFrame.Angles(0, math.rad(-22.5), 0),
center = CFrame.new(),
right = CFrame.Angles(0, math.rad(22.5), 0),
}
-- Cast rays from the head and rootPart in each direction
local rays = {}
for direction, rotation in pairs(directions) do
rays[direction] = {
top = workspace:Raycast(ROOT_PART.Position + Vector3.new(0,3,0), (ROOT_PART.CFrame * rotation * CFrame.new(0,3,0)).LookVector * 9, raycastParams),
bottom = workspace:Raycast(ROOT_PART.Position, (ROOT_PART.CFrame * rotation).LookVector * 9, raycastParams)
}
end
-- Check if the rays in any pair hit the same part
local anyPairMatches = false
for _, rayPair in pairs(rays) do
if rayPair.top and rayPair.bottom and rayPair.top.Instance == rayPair.bottom.Instance then
anyPairMatches = true
break
end
end
-- Update the properties of the new part based on whether any pair matches
if anyPairMatches then
slideMultiplier = 2
else
slideMultiplier = .6
end
end)
HUMANOID.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Jumping then
stopSlide()
end
end)
while wait(0.1) do
if isSliding then
local elapsedTime = tick() - slideStartTime
if elapsedTime >= SLIDE_DURATION then
stopSlide()
else
local slideProgress = elapsedTime / SLIDE_DURATION*slideMultiplier
if ROOT_PART:FindFirstChild("BodyVelocity") then
ROOT_PART.BodyVelocity.Velocity = ROOT_PART.BodyVelocity.Velocity * (1 - slideProgress)
end
end
end
end