I need help to fix my shoulder-surfing script

I need help fixing a shoulder-surfing script i’ve made

so I know how and why there is a problem, but I just have no idea how to fix it

I’ve been working on a shoulder surfing script for a while now and this is the farthest point I’ve gotten to yet I just cannot fix this while keeping it optimized, I make it so it gets the difference between two vectors, the camera and HRP rotations, I use look vectors just so I can easily get the forward facing direction of each one, and it works, but only when my HRP is facing in the z direction, the issue is that I’m only offsetting the camera by the x position, and that means it only looks good when I’m facing the z direction, so what I need help with; is coming up with the “formula” I guess, of how to make it so it adjusts to be the opposite direction of the HRP, now I know technically I can just use ifs and elseifs, but I want to keep the game optimized and running an if every time the camera changed would deoptimize it, even if it’s by a tiny bit, I want maximum optimization, but I feel like there’s a way to do it without ifs, if not please correct me, but otherwise, yea

I would submit a video, but I’m using a laptop right now, so unless you want to try to understand what’s happening in 20fps you might as well test it yourself


local Players = game:GetService("Players")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local multiplier = 1.6

local tweenS = game:GetService("TweenService")
local tweenInf = TweenInfo.new(0.23, Enum.EasingStyle.Quart)

local function onCharacterAdded(character)
	local humanoid = character:WaitForChild("Humanoid")
	local hrp = character:WaitForChild("HumanoidRootPart")
	camera.Changed:Connect(function()
		
		local cameraDirection = camera.CFrame.LookVector
		local hrpDirection = hrp.CFrame.LookVector
		local unit = (cameraDirection - hrpDirection).Unit
		local amount = unit.X * multiplier
		
		local offset = hrp.CFrame:VectorToObjectSpace(Vector3.new(amount * -1, 0, 0))
		--print("CamDir: ", cameraDirection)
		--print("hrpDir: ", hrpDirection)
		
		--print("-----")
		
		--print("Unti: ", unit)
		--print("offset: ", offset)

		local goal = {}
		goal.CameraOffset = offset

		local tween = tweenS:Create(humanoid, tweenInf, goal)
		tween:Play()
	end)
end

player.CharacterAdded:Connect(onCharacterAdded)

if player.Character then
	onCharacterAdded(player.Character)
end

the prints in the script I used to debug and try to figure the problem but they didn’t help at all