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
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
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.