Animations make the character turn while sliding

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.

1 Like

Try setting the Humanoid’s hip height to be slightly raised.

1 Like

Common issue that is still rather hard to solve.

I suggest (as lolbillyyy suggested) setting hip height higher.

depends on what you should do after this but here are some solutions:

  • If you want the player to be able to rotate while sliding then force them into shiftlock or first-person
  • If rotating isnt needed then just set a rotation constraint on the player’s character.

Last idea for a fix: try looking if you mirror the animation that the character also starts rotating the other way. If you make the animation different it might also fix this. (first check is to see if it animation related, but i think already is)

Thanks. Works almost flawlessly. Experimented with it a bit and found that 1/9 of the character’s HumanoidRootPart Y size works the best. Even semi-fixed a weird issue with trimping.

@1gotba_nned0 Thanks for the reply too. The character does start sliding the other way when the animation is mirrored. Maybe has something to do with the center of mass that the animation shifts.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.