How to change CFrame rotation?

Hello, I’m using a viewportFrame and I found this method from another post on how to automatically put an object in a viewportFrame in the correct area

here is the method:


```local function getCameraOffset(fov, extentsSize)
	local halfSize = extentsSize.Magnitude / 2
	local fovDivisor = math.tan(math.rad(fov / 2))
	return halfSize / fovDivisor
end

local function zoomToExtents(camera, instance)
	local isModel = instance:IsA("Model")

	local instanceCFrame = isModel and instance:GetModelCFrame() or instance.CFrame
	local extentsSize = isModel and instance:GetExtentsSize() or instance.Size

	local cameraOffset = getCameraOffset(camera.FieldOfView, extentsSize)
	local CframeP = camera.CFrame.Position
	local cameraRotation = camera.CFrame -  Vector3.new(CframeP.X , CframeP.Y + 3, CframeP.Z)

	local instancePosition = instanceCFrame.p
	camera.CFrame = cameraRotation + instancePosition + (-cameraRotation.LookVector * cameraOffset)
	camera.Focus = cameraRotation + instancePosition
end

the issue with this method is the camera is rotated poorly

How do I rotate the camera with this method so they are in the correct view?

1 Like

You can use the Rotation property. Here’s how you can modify the zoomToExtents function to adjust the camera rotation:

local function zoomToExtents(camera, instance)
    local isModel = instance:IsA("Model")
    
    local instanceCFrame = isModel and instance:GetModelCFrame() or instance.CFrame
    local extentsSize = isModel and instance:GetExtentsSize() or instance.Size
    
    local cameraOffset = getCameraOffset(camera.FieldOfView, extentsSize)
    local cameraRotation = camera.CFrame - camera.CFrame.Position
    
    -- Get the position of the instance
    local instancePosition = instanceCFrame.p
    
    -- Create a new camera rotation based on the position of the instance
    local newCameraRotation = CFrame.lookAt(camera.CFrame.Position, instancePosition)
    
    -- Apply the camera rotation to the camera's CFrame
    camera.CFrame = newCameraRotation + (newCameraRotation.UpVector * 3) + (-newCameraRotation.LookVector * cameraOffset)
    camera.Focus = instanceCFrame
    
end
1 Like

in your method its very far away

1 Like

Ah! One possibility is that the camera’s position is too far away from the instance. The camera’s position is set based on the instance’s position and size, so if the instance is very large or located very far away, the camera might be placed very far away as well. You could try adjusting the camera offset to a smaller value to bring the camera closer to the instance, or modify the camera position directly to get the desired view.

1 Like

I asked chatGPT and it sent me this code:

local function getCameraOffset(fov, extentsSize)
	local halfSize = extentsSize.Magnitude / 2
	local fovDivisor = math.tan(math.rad(fov / 2))
	return halfSize / fovDivisor
end

local function zoomToExtents(camera, instance)
	local isModel = instance:IsA("Model")

	local instanceCFrame = isModel and instance:GetModelCFrame() or instance.CFrame
	local extentsSize = isModel and instance:GetExtentsSize() or instance.Size
	local cameraOffset = getCameraOffset(camera.FieldOfView, extentsSize / 2) - 5
	local CframeP = camera.CFrame.Position
	local cameraRotation = camera.CFrame - Vector3.new(CframeP.X, CframeP.Y + 3, CframeP.Z)

	cameraRotation = cameraRotation * CFrame.fromEulerAnglesXYZ(math.rad(50), math.rad(210), 0)

	local instancePosition = instanceCFrame.p
	camera.CFrame = cameraRotation + Vector3.new(instancePosition.X, instancePosition.Y + 4, instancePosition.Z) + (-cameraRotation.LookVector * cameraOffset) - cameraRotation.LookVector * 2
	camera.Focus = cameraRotation + instancePosition
end

I put it in game and it works good:

2 Likes

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