Rotating camera with TweenService in viewportFrames not working like I want it

Ok so I tried to let the camera (not part/model cause it is less laggier to do it with camera) rotate 180 around a part in the viewportFrame with TweenService, but instead of it rotating around the part it moves to the part and the camera turns away, I have no idea how to fix it and I am stuck with this for days already.

-- Local script:
local part = Instance.new("Part")
part.Material = Enum.Material.Neon
part.BrickColor = BrickColor.new("Really red")
part.Position = Vector3.new(0, 0, 0)
part.Parent = viewportFrame


local camera = Instance.new("Camera")
viewportFrame.CurrentCamera = camera
camera.Focus = part.CFrame
camera.CFrame = part.CFrame:ToWorldSpace(CFrame.new(0, 2, 6))
camera.Parent = viewportFrame

TweenService:Create(camera, TweenInfo.new(6), {CFrame = part.CFrame:ToWorldSpace(CFrame.new(0, 2, 6) * CFrame.Angles(0, math.rad(180), 0)), Focus = part.CFrame}):Play()```
1 Like

I see what your doing, and although TweenService is almost 100% of the time the best way, i think you should use a loop for this one:

local distancefrompart = 10
local heightfrompart = 5

local step = 0

RS.RenderStepped:Connect(function(dt)
     step += 1 * (dt * 60) -- change the 1 addition for orbit speed change (the dt*60 is just so it moves consistently with regards to time, regardless of the framerate of the computer)
     local orbitcframe = CFrame.new(part.Position) * CFrame.Angles(0, math.rad(step % 360), 0) * CFrame.new(0, heightfrompart, -distancefrompart)
     camera.CFrame = CFrame.new(orbitcframe.p, part.Position)
end)
1 Like

Can I also add a NumberValue, tween the value of the NumberValue till 360 and update the camera every Renderstep (just like roblox showed in one of their example codes for camera manipulation)?

actually that is a great idea, then set the step variable equal to the number value’s value. You will also have to constantly retween it when it gets to 360, but that should work perfectly

I already made a function that tweens the camera and sets it back to 0 when the tween is completed and tweens it again (and that’s actually the first time that using recursion was helpful), with the step value I still need to learn that, but still thank you for helping. :smiley:

1 Like