Viewport Camera Rotating Problem

What’s the problem:
I don’t work with cameras or viewports, so this is sorta new to me.
I’m just testing, but the problem I have is that it’s rotating around the object in a weird way.
I want the object to be in the very middle of the viewport and for the camera to rotate around it

Example of how the problem looks

Local Script

local VPF = script.Parent.ViewportFrame
local Object = game:GetService("ReplicatedStorage").Assets.Weapons.Guns.GhostBeater

local R = 0

local function VPFTesting()
	local VPFObj = Object:Clone()
	VPFObj.Parent = VPF
	
	local cam = Instance.new("Camera")
	cam.Parent = VPF
	VPF.CurrentCamera = cam
	
	game:GetService("RunService").Heartbeat:Connect(function()
		cam.CFrame = CFrame.Angles(0,math.rad(R),0) * CFrame.new(Object.Handle.Position + (Object.Handle.CFrame.LookVector*5) + Vector3.new(5,0,0), Object.Handle.Position)
		R = R + 1
	end)
	
end
VPFTesting()

Disclaimer
I got to this point by just following Youtube tutorials, reading Devforum, and going on Developer. I do undertsand what I wrote, but I’m not confident in the fact that my code is perfect. I have looked for solutions already and try to apply it to my own code, but none of them have worked for me.

why are you rotating the camera? why not the object?

Sorry for the late reply, it was because I wanted to be efficient. I’ve read that rotating the object will take more memory and work for the script.

i might be wrong, but it seems like a really bad idea. rotating an object is easier and probably takes less memory

1 Like

Rotating a camera is more performant according to this developer article.

Moving a viewport’s physical children is less performant than keeping them static. If you need to update the view, it’s better to move the camera than move the parts/models.

For @CallCopsIKillTime checkout the placefile

The code should look like this:

--CF is the model position, you can replace it with part.Position or handle.Position.
	local cf, size = model:GetBoundingBox()
	local distance = vpfModel:GetFitDistance(cf.Position) --Calculated by module, or you could set any number

	game:GetService("RunService").RenderStepped:Connect(function(dt)
		theta = theta + math.rad(20 * dt)
		orientation = CFrame.fromEulerAnglesYXZ(math.rad(-20), theta, 0)
		camera.CFrame = CFrame.new(cf.Position) * orientation * CFrame.new(0, 0, distance)
	end)
3 Likes

Thank you for clearing things up, this should be very useful! Also thank you @jaidwe, for the advice too!