Multidirectional movement causes flinging

Not sure where this issue could stem from honestly. I have a feeling its related to my vel variable but I tried changing it, resetting it but I keep having this issue. Insight would be appreciated.

Here’s a video of it happening

Here’s the code:

function Legs:Update(arg1, DeltaTime)
	local Character = self.Character
	if not Character then
		return
	end

	local Humanoid = self.Humanoid
	if not Humanoid or Humanoid.Health <= 0 then
		return
	end
	if not Humanoid.RootPart then
		return
	end

	local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
	if not HumanoidRootPart then
		return
	end

	local vel = Vector3.new(0, 0, 0)
	local HorizontalVelocity = Humanoid.RootPart.AssemblyLinearVelocity * Vector3.new(1, 0, 1) / Humanoid.WalkSpeed

	local ForwardDot = HorizontalVelocity:Dot(Humanoid.RootPart.CFrame.LookVector)
	local RightDot = HorizontalVelocity:Dot(Humanoid.RootPart.CFrame.RightVector)

	local RightHip = arg1.Joints["Right Hip"]
	local LeftHip = arg1.Joints["Left Hip"]
	local RootJoint = arg1.Joints.RootJoint

	if
		not RightHip
		or not RightHip.Parent
		or not LeftHip
		or not LeftHip.Parent
		or not RootJoint
		or not RootJoint.Parent
	then
		return
	end

	if HorizontalVelocity.Magnitude > 0.1 and Humanoid:GetState() ~= Enum.HumanoidStateType.Climbing then
		local Speed = HorizontalVelocity.Magnitude
		vel = (HumanoidRootPart.AssemblyLinearVelocity * Speed + vel * 2) / (Speed + 2)

		local LocalVelocity = HumanoidRootPart.CFrame:pointToObjectSpace(vel + HumanoidRootPart.Position)

		local AngleRight = math.atan(-LocalVelocity.z / LocalVelocity.x)
		if LocalVelocity.x < 0 then
			AngleRight = AngleRight + math.pi
		end

		local AngleLeft = math.pi - math.atan(LocalVelocity.z / LocalVelocity.x)
		if LocalVelocity.x < 0 then
			AngleLeft = AngleLeft + math.pi
		end

		local StepFactor = -RightDot * 0.5
		if RightDot > -0.01 then
			StepFactor = RightDot * 0.5
		end

		local CrouchOffset = CFrame.new()
		if Humanoid:GetAttribute("Crouch_C") or Humanoid:GetAttribute("Crouch") then
			CrouchOffset = CFrame.new(0, 0.75, -0.5)
		end

		local LeftSwing = 1
		local RightSwing = 0.35
		if RightDot < 0 then
			LeftSwing = 0.35
			RightSwing = 1
		end

		LeftHip.C1 = LeftHip.C1:Lerp(
			CFrame.new(0, 1, 0) * CFrame.Angles(0, AngleRight + math.pi, 0),
			math.clamp(0.2 * DeltaTime, 0, 1)
		)
		RightHip.C1 = RightHip.C1:Lerp(
			CFrame.new(0, 1, 0) * CFrame.Angles(0, AngleLeft + math.pi, 0),
			math.clamp(0.2 * DeltaTime, 0, 1)
		)

		LeftHip.C0 = LeftHip.C0:Lerp(
			CFrame.new(-0.5, -1, 0)
				* CrouchOffset
				* CFrame.Angles(0, AngleRight - math.pi, 0)
				* CFrame.new(StepFactor * 0.5, 0, StepFactor * LeftSwing * RightDot),
			math.clamp(0.2 * DeltaTime, 0, 1)
		)

		RightHip.C0 = RightHip.C0:Lerp(
			CFrame.new(0.5, -1, 0)
				* CrouchOffset
				* CFrame.Angles(0, AngleLeft - math.pi, 0)
				* CFrame.new(-StepFactor * 0.5, 0, -StepFactor * RightSwing * RightDot),
			math.clamp(0.2 * DeltaTime, 0, 1)
		)

		RootJoint.C0 = RootJoint.C0:Lerp(
			CFrame.new(0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 1, 0)
				* CFrame.Angles(math.rad(ForwardDot * 5), math.rad(-RightDot * 5), 0),
			math.clamp(0.2 * DeltaTime, 0, 1)
		)
	end
end
1 Like

The math for your vel calculation looks like it could be creating massive spikes in velocity when Speed changes rapidly. You’re multiplying AssemblyLinearVelocity by Speed and then dividing by Speed + 2, which is a weird way to weight it and likely causes the physics engine to freak out during direction changes. Check if the flinging happens specifically when HorizontalVelocity.Magnitude drops near zero or shifts suddenly.

Bump, still need help with this

1 Like