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)