Trouble with Offsetting Camera

I want to center the camera so that the glowing part aligns in the middle of the partially transparent frame.

As shown in the image the glowing block is not centered:

I’m not sure what I’m doing wrong. This is the code I’m using:

function module:ADVLookAt(cframe, posOffset, viewOffset, uiFrame, Save)
	local part = Instance.new("Part")
	part.Size = Vector3.new(3,3,3)
	part.Material = Enum.Material.Neon
	part.Anchored = true
	part.Parent = workspace
	
	Global.CurrentCameraSet = "Shop"

	local Camera = workspace.CurrentCamera
	local targetWorldPos = cframe.Position + viewOffset
	local initialCamPos = cframe.Position + posOffset

	-- Step 1: Setup initial lookAt
	local camCFrame = CFrame.lookAt(initialCamPos, targetWorldPos)
	Camera.CFrame = camCFrame

	-- Step 2: Project the target point to screen space
	local screenPoint, onScreen = Camera:WorldToViewportPoint(targetWorldPos)
	if not onScreen then return end

	-- Step 3: Find offset between target's screen position and frame center
	local frameCenter = uiFrame.AbsolutePosition + uiFrame.AbsoluteSize / 2
	local offsetPixels = Vector2.new(screenPoint.X, screenPoint.Y) - frameCenter

	-- Step 4: Convert pixel offset to world offset
	local distance = (initialCamPos - targetWorldPos).Magnitude
	local fovY = math.rad(Camera.FieldOfView)
	local pixelsPerStudY = Camera.ViewportSize.Y / (2 * math.tan(fovY / 2) * distance)
	local pixelsPerStudX = pixelsPerStudY * (Camera.ViewportSize.X / Camera.ViewportSize.Y)

	local rightVector = camCFrame.RightVector
	local upVector = camCFrame.UpVector
	local localOffset = (rightVector * (offsetPixels.X / pixelsPerStudX)) - (upVector * (offsetPixels.Y / pixelsPerStudY))

	-- Step 5: Apply offset and update camera
	local adjustedCamPos = initialCamPos - localOffset
	local finalCFrame = CFrame.lookAt(adjustedCamPos, targetWorldPos)
	Camera.CFrame = finalCFrame

	if Save then
		SavedCameraSetting = finalCFrame
	end
	
	part.CFrame = cframe
	game:GetService("Debris"):AddItem(part, 5)
end

The view offset is always set to Vector3.new(0,0,0) and needs to be deleted.
The posOffset varies between Vector3.new(-60, 10, 0), Vector3.new(15, 9, -15), Vector3.new(40, 9, -40), and Vector3.new(-5, 0, 5) depending on the model being displayed.
cframe is the point I’m trying to line up in the middle of the uiframe.
uiframe is the semi-transparent frame.

In what way is it not centered
The middle of the part is around 603 pixels across and the image size is 1210, which makes the actual center at 605

I’ve edited the script once more and I’m very close! But there is something still quite off.

As shown in the image the objects now align on the local x axis but I can’t seem to get it to correctly center on the y axis. The object should be in the center of the square. Here is my code:

Okay I figured out the issue. I was trying to correct the local y axis of the camera when it was already centered on the object.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.