Custom character rotation clips around weirdly

I’m trying to script custom character things to replace Humanoid. However, I can’t get rotation correct. Here is the code:

RS.Stepped:Connect(function(Delta: number)
	-- For space, removed fully functional things and got straight to the point
	local InputDirection: Vector2 = Vector2.new(
			(UIS:IsKeyDown(Enum.KeyCode.D) and 1 or 0) - (UIS:IsKeyDown(Enum.KeyCode.A) and 1 or 0),
			(UIS:IsKeyDown(Enum.KeyCode.S) and 1 or 0) - (UIS:IsKeyDown(Enum.KeyCode.W) and 1 or 0)
	)
	
	local Direction: Vector3 = Vector3.new(InputDirection.X, 0, InputDirection.Y)
	
	if Direction.Magnitude > 0 then
		Controller.MovingDirection = Direction.Unit

		-- Lines below here are the issue
		local RY: number = math.rad(PrimaryPart.Rotation.Y)
		local _, Angle: number = CFrame.lookAt(PrimaryPart.Position, PrimaryPart.Position + Direction.Unit):ToEulerAnglesYXZ()
		Angle = EM.AngleLerp(RY, Angle, 0.25)
		PrimaryPart.CFrame = CFrame.new(PrimaryPart.Position) * CFrame.Angles(0, Angle, 0)
	else
		Controller.MovingDirection = Vector3.zero
	end
end)

The AngleLerp function:

-- Lerp angles with wrapping. Uses radians.
M.AngleLerp = function(A: number, B: number, Factor: number): number
	local Difference: number = (B - A) % (math.pi * 2)
	if Difference > math.pi then
		Difference -= math.pi * 2
	elseif Difference < -math.pi then
		Difference += math.pi * 2
	end
	return A + Difference * Factor
end

This video will better explain hopefully:


I have not gotten to a solution. I would assume the AngleLerp function would work fine since I got it online but it seems to contribute to the issue somehow.

1 Like

If it helps it can’t go between -102.5 and 107 degrees. Please help.

I have not gotten to a solution. I would assume the AngleLerp function would work fine since I got it online but it seems to contribute to the issue somehow.

so it works fine without AngleLerp()?

why do you need wrapping in the AngleLerp function, it literally limits your angles i think? Try math.lerp instead

Lerps don’t work either and the wrapping is because angles wrap. The angle lerp function is not by me but it appears correct

?

postmustbeatleast30characters

It works fine without AngleLerp