Custom plane goes all the way around when facing backwards

I made this custom plane flying script but when my Y rotation goes over 180 degrees or below -180 degrees it turns around, I am aware that using CFrame:Lerp() to control the plane would fix this issue but I prefer using a spring module as it is both smooth and not affected by frame rates (before I was essentially doing “currentCFrame:Lerp(targetCFrame, delta * speed)” but then the plane turns slower the higher your frame rate is)

So essentially, how would I fix this without using CFrame:Lerp()?

Video of the problem:

My code:

local runService = game:GetService("RunService")

local springModule = require(game.ReplicatedStorage:WaitForChild("SpringModule"))
local rotationSpringX = springModule.new()
rotationSpringX.Speed = 7
rotationSpringX.Damper = 0.7

local rotationSpringY = springModule.new()
rotationSpringY.Speed = 5
rotationSpringY.Damper = 0.7

local rotationSpringZ = springModule.new()
rotationSpringZ.Speed = 12
rotationSpringZ.Damper = 0.9

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local plane = player.Character

local function Lerp(num, goal, i)
	return num + (goal-num)*i
end

runService.RenderStepped:Connect(function(delta)
	local targetRotationX, targetRotationY, targetRotationZ = camera.CFrame:ToOrientation()
	local targetRotation = Vector3.new(math.deg(targetRotationX) + 10, math.deg(targetRotationY), math.deg(targetRotationZ))
	
	rotationSpringX.Target = targetRotation.X
	rotationSpringY.Target = targetRotation.Y
	
	rotationSpringZ.Target = math.clamp(rotationSpringY.Velocity / 2, -90, 90)
	
	local rotationCFrame = CFrame.fromOrientation(math.rad(rotationSpringX.Position), math.rad(rotationSpringY.Position), math.rad(rotationSpringZ.Position))
	
	plane:SetPrimaryPartCFrame(CFrame.new(plane.PrimaryPart.Position) * rotationCFrame)
end)
4 Likes