I’m trying to code a free camera and I’m getting this issue. It’s something to do with the OrgCamRotY
and I’m not sure what.
Issue in question:
code:
local PLAYERS = game:GetService("Players")
local USER_INPUT_SERVICE = game:GetService("UserInputService")
local RUN_SERVICE = game:GetService("RunService")
-- Player
local Player = PLAYERS.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local CurrentCamera = game.Workspace.CurrentCamera
local PlayerControls = require(Player.PlayerScripts:WaitForChild("PlayerModule")):GetControls()
local freecam = {}
freecam.Enabled = false
freecam.CameraPosition = Vector3.new()
freecam.CameraOrientation = Vector3.new()
local KeysDown = {}
local function CFrameToOrientation(cframe: CFrame)
local rx, ry, rz = cframe:ToEulerAnglesYXZ()
return Vector3.new(math.deg(rx, math.deg(ry), math.deg(rz)))
end
local function OrientationToCFrame(Orientation: Vector3)
local dx, dy, dz = Orientation.X, Orientation.Y, Orientation.Z
local rx, ry, rz = math.rad(dx), math.rad(dy), math.rad(dz)
return rx, ry, rz
end
function freecam.Toggle()
if freecam.Enabled == true then
CurrentCamera.CameraType = Enum.CameraType.Scriptable
PlayerControls:Disable()
freecam.CameraPosition = CurrentCamera.CFrame.Position
freecam.CameraOrientation = CFrameToOrientation(CurrentCamera.CFrame)
else
CurrentCamera.CameraType = Enum.CameraType.Custom
USER_INPUT_SERVICE.MouseBehavior = Enum.MouseBehavior.Default
PlayerControls:Enable()
end
end
USER_INPUT_SERVICE.InputBegan:Connect(function(inputObject, gameProcessed)
if gameProcessed then return end
local Key = inputObject.KeyCode
if Key == Enum.KeyCode.Z then
freecam.Enabled = not freecam.Enabled
freecam.Toggle()
end
KeysDown[Key] = true
end)
USER_INPUT_SERVICE.InputEnded:Connect(function(inputObject)
local Key = inputObject.KeyCode
KeysDown[Key] = nil
end)
RUN_SERVICE:BindToRenderStep("FreeCam", Enum.RenderPriority.Input.Value + 1, function(delta)
if not Character or not Character:FindFirstChild("Humanoid") or (Character.Humanoid and Character.Humanoid.Health <= 0) then
RUN_SERVICE:UnbindFromRenderStep("FreeCam")
end
if freecam.Enabled == true then
if USER_INPUT_SERVICE:IsMouseButtonPressed(Enum.UserInputType.MouseButton2) then
USER_INPUT_SERVICE.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
else
USER_INPUT_SERVICE.MouseBehavior = Enum.MouseBehavior.Default
end
local MouseDelta = -USER_INPUT_SERVICE:GetMouseDelta()/3
local OrgCamRotX, OrgCamRotY, OrgCamRotZ = OrientationToCFrame(freecam.CameraOrientation)
local NewPosition = freecam.CameraPosition
local NewCFrameRotation = CFrame.fromEulerAnglesYXZ(math.clamp(OrgCamRotX + math.rad(MouseDelta.Y), -1.4, 1.4), OrgCamRotY + math.rad(MouseDelta.X), OrgCamRotZ)
local Speed = delta * 15
if KeysDown[Enum.KeyCode.W] then
NewPosition += (NewCFrameRotation.LookVector*Speed)
end
if KeysDown[Enum.KeyCode.S] then
NewPosition += (-NewCFrameRotation.LookVector*Speed)
end
if KeysDown[Enum.KeyCode.D] then
NewPosition += (NewCFrameRotation.RightVector*Speed)
end
if KeysDown[Enum.KeyCode.A] then
NewPosition += (-NewCFrameRotation.RightVector*Speed)
end
if KeysDown[Enum.KeyCode.E] then
NewPosition += (NewCFrameRotation.UpVector*Speed)
end
if KeysDown[Enum.KeyCode.Q] then
NewPosition += (-NewCFrameRotation.UpVector*Speed)
end
local NewCameraCFrame = CFrame.new(NewPosition) * NewCFrameRotation
freecam.CameraPosition = NewPosition
freecam.CameraOrientation = CFrameToOrientation(NewCFrameRotation)
CurrentCamera.CFrame = NewCameraCFrame
end
end)
return freecam
The code causing the issue (I think)
local MouseDelta = -USER_INPUT_SERVICE:GetMouseDelta()/3
local OrgCamRotX, OrgCamRotY, OrgCamRotZ = OrientationToCFrame(freecam.CameraOrientation)
local NewPosition = freecam.CameraPosition
local NewCFrameRotation = CFrame.fromEulerAnglesYXZ(math.clamp(OrgCamRotX + math.rad(MouseDelta.Y), -1.4, 1.4), OrgCamRotY + math.rad(MouseDelta.X), OrgCamRotZ)
local Speed = delta * 15
if KeysDown[Enum.KeyCode.W] then
NewPosition += (NewCFrameRotation.LookVector*Speed)
end
if KeysDown[Enum.KeyCode.S] then
NewPosition += (-NewCFrameRotation.LookVector*Speed)
end
if KeysDown[Enum.KeyCode.D] then
NewPosition += (NewCFrameRotation.RightVector*Speed)
end
if KeysDown[Enum.KeyCode.A] then
NewPosition += (-NewCFrameRotation.RightVector*Speed)
end
if KeysDown[Enum.KeyCode.E] then
NewPosition += (NewCFrameRotation.UpVector*Speed)
end
if KeysDown[Enum.KeyCode.Q] then
NewPosition += (-NewCFrameRotation.UpVector*Speed)
end
local NewCameraCFrame = CFrame.new(NewPosition) * NewCFrameRotation
freecam.CameraPosition = NewPosition
freecam.CameraOrientation = CFrameToOrientation(NewCFrameRotation)
CurrentCamera.CFrame = NewCameraCFrame