Hello ! I’ve been recently writing a custom implementation for my camera, but I’ve encountered weird issues when editing character’s CFrame.
Consider the code as the following:
local cameraAPI = {}
-- Cache variables and shorthands, includes math functions as well
local CFRAME = CFrame.new
local ANGLES = CFrame.Angles
local PI = math.pi
local DEG = math.deg
local RAD = math.rad
local CLAMP = math.clamp
-- Get services
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local players = game:GetService("Players")
-- Constants
local CAM_OFFSET = Vector3.new(0, 0.85, 1)
local Y_AXIS_LOCK = 75 -- maximum angle along Y axis
local X_SENSITIVITY = 0.015
local Y_SENSITIVITY = 0.075
-- Define misc variables
local currentCharacter
local currentCamera
local inputListener
local xAngle, yAngle = 0, 0
-- Body and functions
local function update(dt)
local rootPart = currentCharacter.PrimaryPart
local yOrientation = ANGLES(0, RAD(xAngle), 0)
local displaced = rootPart.CFrame * CAM_OFFSET
local cf = yOrientation * ANGLES(RAD(yAngle), 0, 0) + displaced
print(rootPart.Position) -- Position doesn't give weird results, the CFrame is identic while idling
rootPart.CFrame = yOrientation + rootPart.Position
currentCamera.CFrame = cf
end
local function mouseMoved(input, robloxHandled)
if input.UserInputType.Value ~= 4 or robloxHandled then return end
xAngle = (xAngle - input.Delta.X * X_SENSITIVITY * PI) % 360
yAngle = CLAMP(yAngle - input.Delta.Y * Y_SENSITIVITY * PI, -Y_AXIS_LOCK, Y_AXIS_LOCK)
end
-- Module
function cameraAPI:Init(character)
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
players.LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson
currentCamera = camera
currentCharacter = character
if inputListener and typeof(inputListener) == "RBXScriptConnection" then
inputListener:Disconnect()
end
inputListener = userInputService.InputChanged:Connect(mouseMoved)
runService:BindToRenderStep("HandleCamera", Enum.RenderPriority.Camera.Value, update)
end
function cameraAPI:Stop()
end
return cameraAPI
As shown in the video below, the camera starts to act unexpectedly as the character spins around for some reasons.
I think I mentioned everything, help would be appreciated !