My goal is to achieve a sliding animation.
The issue is that when loading the animation, my character’s limbs that don’t touch the ground ragdoll.
I looked at the lines that could possibly be causing this issue, like “body velocity”, but I omitted a specific amount of values, or all of the values.
This is the code:
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local PLAYER = Players.LocalPlayer
local CHARACTER = PLAYER.Character or PLAYER.CharacterAdded:Wait()
local HUMANOID = CHARACTER:WaitForChild("Humanoid")
local ROOT_PART = CHARACTER:WaitForChild("HumanoidRootPart")
HUMANOID.CameraOffset = Vector3.new(0, 0, 0)
local ANIMATION_ID = "rbxassetid://103882115862186"
local animation = Instance.new("Animation")
animation.AnimationId = ANIMATION_ID
local animationTrack = HUMANOID:LoadAnimation(animation)
local isSliding = false
local slideStartTime = 0
local SLIDE_DURATION = 1
local slideMultiplier = .6
local SPRINT_SPEED = 25
local WALK_SPEED = 16
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()
end
end
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, 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()
end
end
end
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)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {CHARACTER}
local RunService = game:GetService("RunService")
game:GetService("RunService").Heartbeat:Connect(function()
local directions = {
left = CFrame.Angles(0, math.rad(-22.5), 0),
center = CFrame.new(),
right = CFrame.Angles(0, math.rad(22.5), 0),
}
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
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
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