I need help figuring out how to make auto-centering for my custom camera.
Here is my camera script:
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local camera = game:GetService("Workspace").CurrentCamera
local playerInstances = player:WaitForChild("PlayerInstances")
local playerValues = playerInstances:WaitForChild("PlayerValues")
local cameraValues = playerValues:WaitForChild("CameraValues")
local cameraUpdateable = cameraValues:WaitForChild("CameraUpdateable")
local cameraMode = cameraValues:WaitForChild("CameraMode")
local cameraExtraOffset = cameraValues:WaitForChild("CameraExtraOffset")
local xAngle = 0
local yAngle = 0
local yAngleLimit = 90
local yAngleLimit = 75
local startCFrameObject
local cameraBaseObject
local startCFrame
local cameraCFrame
local cameraFocus
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseIconEnabled = false
UserInputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
xAngle = xAngle-input.Delta.X * 0.4
yAngle = math.clamp(yAngle-input.Delta.Y * 0.4,-yAngleLimit,yAngleLimit)
end
end)
camera.CameraType = Enum.CameraType.Scriptable
cameraUpdateable.Value = true
RunService.RenderStepped:Connect(function()
if cameraUpdateable.Value then
startCFrameObject = character:WaitForChild("Head")
cameraBaseObject = character:WaitForChild("HumanoidRootPart")
startCFrame = CFrame.new(startCFrameObject.CFrame.p) * CFrame.Angles(0, math.rad(xAngle), 0) * CFrame.Angles(math.rad(yAngle), 0, 0)
cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraBaseObject.Size.X, 0, ((cameraBaseObject.Size.Z * 4) * (cameraMode.Value)) + (cameraBaseObject.Size.Z * cameraExtraOffset.Value)))
cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraBaseObject.Size.X, 0, 0))
camera.CFrame = CFrame.new(cameraCFrame.p, cameraFocus.p)
end
end)
Here is a video which showcases what currently happens:
robloxapp-20240623-1038480.wmv (2.6 MB)
Thank you.