I made a script for sliding which works fine without any animations. As soon as I apply one though, the character for some reason starts shifting its direction on uneven surfaces.
This does not happen when I’m using shiftlock, but is there any better way to prevent this?
Script
--part of a local script in ReplicatedFirst
function _G.map(n, start, stop, newStart, newStop, withinBounds)
local value = ((n - start) / (stop - start)) * (newStop - newStart) + newStart
if not withinBounds then
return value
end
if newStart < newStop then
return math.max(math.min(value, newStop), newStart)
else
return math.max(math.min(value, newStart), newStop)
end
end
--Local Script in StarterPlayerCharacter
local slideForce = 50
local slideReduced = 0
local slideDownward = 1.045
local slideTraction = 5
local char = script.Parent
local hum:Humanoid = char:WaitForChild("Humanoid")
local animator:Animator = hum:WaitForChild("Animator")
local hrp:Part = char:WaitForChild("HumanoidRootPart")
local torso:Part = char:WaitForChild("Torso")
...
local yDiff = lastCf.Position.Y - hrp.CFrame.Y
if hum.FloorMaterial == Enum.Material.Air then
yDiff = 0
end
if yDiff > 0 then
yDiff *= 1.5
end
local moveDir = _G.getMoveVector()
local dirForce = (hrp.CFrame * CFrame.Angles(0, math.rad(-moveDir.X * 30), 0)).LookVector * ((slideForce - slideReduced) * _G.map(hum.WalkSpeed, 15, 30, 1, 1.4))
local newY = hrp.AssemblyLinearVelocity.Y
if newY < 0 then
newY *= slideDownward
end
local applyingForce = Vector3.new(
dirForce.X,
newY,
dirForce.Z
)
hrp.AssemblyLinearVelocity = applyingForce
local magnitude = applyingForce.Magnitude
slideSound.PlaybackSpeed = _G.map(magnitude, 0, 50, 0, 1)
slideReduced = math.clamp(slideReduced + (slideTraction * dt) - yDiff, -(slideForce * 2), slideForce)
I tried turning off the limbs’ collision, making them massless, even deleting them, but nothing worked.
I should also note that if the animation turns the torso even slightly, the direction gets messed up on even surfaces too.