Help with plane system

So I want to fix the plane from tilting when the pitch is 0 because right now, it keeps tilting after the player stops turning the plane.

local RunService = game:GetService("RunService")

local model = script.Parent
local FlyPart = model:WaitForChild("FlyPart")
local VehicleSeat = model:WaitForChild("VehicleSeat")

local AngularVelocity = FlyPart:WaitForChild("AngularVelocity")

local flySpeed = 50
local turnSpeed = 1

local pitchSensitivity = 20
local yawSensitivity = 1
local pitchDamping = 0.95
local yawDamping = 0.95

local pitch = 0
local yaw = 0
local targetThrottle = 0

local function Fly()
	local input = VehicleSeat.Steer
	local throttle = VehicleSeat.Throttle

	-- Calculate the desired pitch and yaw based on player input
	local targetPitch = math.rad(input * pitchSensitivity)
	local targetYaw = -input * yawSensitivity * turnSpeed
	
	--print(pitch)
	
	yaw = yaw * yawDamping + targetYaw * (1 - yawDamping)

	if input == 0 then
		pitch = pitch * pitchDamping * 0.02  -- Decrease yaw when the input is 0
		
		if pitch == 0 then
			AngularVelocity.AngularVelocity = Vector3.new() -- Reset the angular velocity when pitch is 0
		end
	else
		pitch = pitch * pitchDamping + targetPitch * (1 - pitchDamping)
	end

	-- Gradual throttle acceleration and deceleration
	if throttle > targetThrottle then
		targetThrottle = math.min(throttle, targetThrottle + 0.02) -- Increase throttle smoothly
	else
		targetThrottle = math.max(throttle, targetThrottle - 0.02) -- Decrease throttle smoothly
	end
	
	-- Adjust the fly part's velocity based on the direction of the turn

	local forwardVelocity = FlyPart.CFrame.LookVector * flySpeed
	local turnVelocity = FlyPart.CFrame.RightVector * yaw
	local finalVelocity = forwardVelocity + turnVelocity
	FlyPart.AssemblyLinearVelocity = finalVelocity

	-- Create a new Vector3 representing the desired angular velocity
	local angularVelocity = Vector3.new(pitch, yaw, targetThrottle)
	AngularVelocity.AngularVelocity = angularVelocity
end

RunService.Heartbeat:Connect(function()
	Fly()
end)

2 Likes