CFrame glitching out when jet points straight up

My jet is flying smoothly, however when it points straight up or down, it starts spinning like crazy. Is there any way this issue can be resolved? I tried using CFrame.fromMatrix instead of CFrame.lookAt but they both had the same results.

Script (Local):

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
local humanoid = character:WaitForChild("Humanoid")
local mouse = player:GetMouse()

local isFlying = false

local jet
local jetSeat
local leaveJet
local remotes

local CAS = game:GetService("ContextActionService")
local UIS = game:GetService("UserInputService")
local runService = game:GetService("RunService")

function onSit(seated, seat)
	if seated then
		remotes = seat.Parent:FindFirstChild("Remotes")
		jetSeat = seat
		jet = seat:FindFirstAncestor("Warzone Jet")
		mouse.TargetFilter = workspace
		
		if remotes then
			leaveJet = remotes.LeaveJet
			
			changeCamera(Enum.CameraType.Follow)
			CAS:BindAction("Leave", leave, false, Enum.KeyCode.E, Enum.KeyCode.Space)
			CAS:BindAction("Start", startJet, false, Enum.KeyCode.Q)
		end
	else
		CAS:UnbindAction("Leave")
		CAS:UnbindAction("Start")
		runService:UnbindFromRenderStep("Fly")
		changeCamera(Enum.CameraType.Custom)
		
		jetSeat = nil
		jet = nil
		mouse.TargetFilter = nil
	end
end

function changeCamera(state)
	repeat wait()
		camera.CameraType = state
	until camera.CameraType == state
end

function leave(action, inputState)
	if inputState == Enum.UserInputState.Begin then
		if action == "Leave" then
			leaveJet:FireServer()
		end
	end
end

function startJet(action, inputState)
	if inputState == Enum.UserInputState.Begin then
		if action == "Start" then
			isFlying = not isFlying
			
			if isFlying then
				jet.PrimaryPart.BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
				runService:BindToRenderStep("Fly", 1, fly)
			else
				jet.PrimaryPart.BodyVelocity.MaxForce = Vector3.new(0, 0, 0)
				runService:UnbindFromRenderStep("Fly")
			end
		end
	end
end

local function getCFrame(position, lookAt)
	return CFrame.lookAt(position, lookAt)
end

function fly()
	jet.PrimaryPart.CFrame = getCFrame(jet.PrimaryPart.Position, mouse.Hit.Position)
	
	if jet.PrimaryPart.BodyVelocity then
		jet.PrimaryPart.BodyVelocity.Velocity = mouse.Hit.LookVector * 125
	end
end

humanoid.Seated:Connect(onSit)
1 Like

The problem you are experiencing isn’t due to the CFrame acting incorrectly. It is rather due to the movement restrictions placed on the camera. I do not know the solution. but I recommend checking out these articles:

I would also recommend checking out EgoMoose’s Wall Stick Controller. I have used it, and whenever your character sticks is walking up a parts surface the camera movement restrictions are adjusted so that they are perpendicular to the parts surface. Here you go:

By the way, here is how the jet flies: https://gyazo.com/fca4b570e660bfcba53a500f54f52846

Is it spinning like that because of CFrame or camera restrictions?

Mostly because the camera restrictions makes the camera rotate its view horizontally instead of vertically if you try to look too far up. This causes the Mouse.Hit.Position to change to change its Position to one that isn’t as close. Because the player is attached to the vehicle the dramatic change in direction causes Mouse.Hit.Position to change its Position to one that isn’t as close AGAIN. This causes another dramatic change in direction and thus becomes a self-perpetuating cycle.

2 Likes