Figuring out some Camera CFrame math [BOUNTY: 50R$]

Currently I have a camera script that acts like a standard third person camera controller.

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Client = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera
local Char = Client.Character or Client.CharacterAdded:Wait()
local hrp = Char:WaitForChild("HumanoidRootPart")

--

local sens = 60
local x = 0
local y = 0

--

local offset = CFrame.new(4, 3, 15)

--

UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

workspace.CurrentCamera.CameraType = "Scriptable"

--

UserInputService.InputChanged:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseMovement then
        x = x + input.Delta.X/sens
        y = math.clamp(y-(input.Delta.Y/sens), -1, 1)
    end
end)

--

RunService:BindToRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value, function(dt)
    local offset = Client.cameraOffset.Value;
    local getCameraRot = CFrame.new(hrp.Position) * CFrame.Angles(0, -x, 0) * CFrame.Angles(y, 0, 0)
    game:GetService("TweenService"):Create(workspace.CurrentCamera, TweenInfo.new(0.05), {CFrame = getCameraRot * offset}):Play();
end)

The script itself is simple enough to understand. I have another script that can turn the camera to face something, and once the camera returns to third person mode, I need it to keep the new position (facing at the thing). Right now, when the facing part behaviour ends, the camera reverts to its original position before the facing part script take over. I need to modify x and y so that it can changes depending on the new Camera CFrame, so in the end CameraRot correctly changes into the new position.
How would I achieve this?

1 Like