Make camera look at part while rotating around origin

I want to make the camera continuously face a characters HumanoidRootPart while also rotating around an origin point and moving closer / farther to maintain a certain distance from the part its looking at.

I currently have the code to make the camera face the part but i dont know how to make it move closer or farther from the origin while doing so.

Code:

local camOrigin = cam.CFrame.Position
		
local CamFollow = RunServ.RenderStepped:Connect(function()
			
	if killedby.Character and killedby.Character:FindFirstChild("HumanoidRootPart") then
				
		local hrp = killedby.Character.HumanoidRootPart
				
		cam.CFrame = CFrame.new(camOrigin, hrp.Position)
				
	end
end)

Bad Visual Representation of what im trying to achieve:


basically the camera should always maintain the same distance from the player while also rotating around the origin

2 Likes

Take the distance from the origin and the root part, get the unit vector then multiply by how many studs you want away from the root part, then subtract it from the root part’s position.

local hrpPos = hrp.Position
local distance = hrpPos - camOrigin
cam.CFrame = CFrame.new(hrpPos - distance.Unit * 5, hrpPos)

Bad visual:
image

2 Likes