So I updated my slope system (from before) and I came across a problem. When my player goes down a slope, it goes really crazy. And it doesn’t face the direction it’s supposed to be in.
Script (Local)
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local slideSound = script.Slide
local slideKeybind = Enum.KeyCode.C
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid") :: Humanoid
local animator = humanoid:WaitForChild("Animator")
local head = character:WaitForChild("Head") :: BasePart
local basePart = humanoid.RootPart
--local slideDuration = 1
local sliding = false
local fakeAccel = 1
local slideDistance = 60
local dynamicSlideDistance = slideDistance
local originalHipHeight = humanoid.HipHeight
local slideTrack = animator:LoadAnimation(script:WaitForChild("SlideAnim"))
local jumpTrack = animator:LoadAnimation(script:WaitForChild("LongJump"))
slideTrack.Looped = true
slideTrack.Priority = Enum.AnimationPriority.Action
local params = RaycastParams.new()
params.FilterDescendantsInstances = {character}
params.FilterType = Enum.RaycastFilterType.Exclude
params.IgnoreWater = true
local lastStep = Vector3.zero
local function enterSlide()
slideSound:Play()
sliding = true
humanoid.AutoRotate = false
--humanoid.HipHeight = 0
-- must be set before updating walkspeed to 0
dynamicSlideDistance = slideDistance + humanoid.WalkSpeed * 1.2 -- so if you're running you slide further
fakeAccel = dynamicSlideDistance / 2
humanoid.WalkSpeed = 0
humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, false)
humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
slideTrack:Play()
end
local function leaveSlide()
slideSound:Stop()
sliding = false
humanoid.AutoRotate = true
humanoid.HipHeight = originalHipHeight
humanoid.WalkSpeed = game.StarterPlayer.CharacterWalkSpeed
dynamicSlideDistance = slideDistance
slideTrack:Stop()
end
local earlierTime = 0
local cooldownTime = 0
local function canSlide()
local timeNow = os.clock()
if not humanoid or humanoid.Health <= 0 then
return false
end
if humanoid.FloorMaterial == Enum.Material.Air then
return false
end
-- to prevent them from using 'slide' if they are not moving
if humanoid.MoveDirection == Vector3.zero then
return false
end
earlierTime = timeNow + cooldownTime
return true
end
local function slide(_, state: Enum.UserInputState, input: InputObject)
if not canSlide() then return end
if state == Enum.UserInputState.Begin then
enterSlide()
end
end
local function updateSlide(dt: number)
if not sliding then return end
slideSound.PlaybackSpeed = character.PrimaryPart.Velocity.Magnitude * 0.05
local ray = workspace:Raycast((head.CFrame * CFrame.new(0, -1, 0)).Position, head.CFrame.LookVector * 2, params)
--[[if fakeAccel < 0.8 and ray ~= nil then
leaveSlide()
end]]
fakeAccel = math.clamp(fakeAccel - 0.005, 0, 1)
--[[if fakeAccel == 0 then
leaveSlide()
end]]
basePart:ApplyImpulse(basePart.CFrame.UpVector * -75)
local goal = basePart.AssemblyLinearVelocity:Lerp(basePart.CFrame.LookVector * dynamicSlideDistance, fakeAccel)
if goal ~= goal then
return
end
basePart.AssemblyLinearVelocity = goal
end
ContextActionService:BindAction("slide", slide, true, slideKeybind)
RunService.RenderStepped:Connect(updateSlide)
humanoid.Jumping:Connect(function()
if sliding == true then
jumpTrack:Play()
leaveSlide()
end
end)
humanoid.Touched:Connect(function()
if sliding == false then
jumpTrack:Stop()
end
end)