Camera Manipulation Script Rotates Around The Character, Not Part

Hi, I’m not the best physics scripter, but I need the CurrentCamera of a player to rotate around a desired object. I have a script for it, but the CurrentCamera rotates around the origin, or in this case, the character.

Here’s my script:

    local target = game.Workspace.ViewPart
	local camera = workspace.CurrentCamera
	camera.CameraType = Enum.CameraType.Scriptable
	camera.Focus = target.CFrame
	camera.CameraSubject = target
	camera.CFrame = target.CFrame
	local rotationAngle = Instance.new("NumberValue")
	local tweenComplete = false
	
	local cameraOffset = target.Position
	local rotationTime = 15  
	local rotationDegrees = 360
	local rotationRepeatCount = -1
	local lookAtTarget = true
	 
	local function updateCamera()
		local rotatedCFrame = CFrame.Angles(0, math.rad(rotationAngle.Value), 0)
		if lookAtTarget == true then
			camera.CFrame = rotatedCFrame:ToObjectSpace(CFrame.new(cameraOffset))
			camera.CFrame = CFrame.new(camera.CFrame.Position, target.Position)
		else
			camera.CFrame = rotatedCFrame:ToObjectSpace(CFrame.new(cameraOffset))
		end
	end
	 
	local tweenInfo = TweenInfo.new(rotationTime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, rotationRepeatCount)
	local tween = TweenService:Create(rotationAngle, tweenInfo, {Value=rotationDegrees})
	tween.Completed:Connect(function()
		tweenComplete = true
	end)
	tween:Play()
	 
	RunService.RenderStepped:Connect(function()
		if tweenComplete == false then
			updateCamera()
		end
	end)

Thanks in the meantime,
Edward

The script is originally from a roblox dev hub page, a minor edit to the rotatedCFrame and cameraOffset:

	
    local TweenService = game:GetService("TweenService")
    local RunService = game:GetService("RunService")
     
    local target = workspace.ViewPart
    local camera = workspace.CurrentCamera
    camera.CameraType = Enum.CameraType.Scriptable
    camera.Focus = target.CFrame
    local rotationAngle = Instance.new("NumberValue")
    local tweenComplete = false
     
    local cameraOffset = Vector3.new(0, 0, 12)
    local rotationTime = 2  -- Time in seconds
    local rotationDegrees = 360
    local rotationRepeatCount = -1  -- Use -1 for infinite repeats
    local lookAtTarget = false  -- Whether the camera tilts to point directly at the target
     
    local function updateCamera()
    	local rotatedCFrame = CFrame.new(target.Position) * CFrame.Angles(0, math.rad(rotationAngle.Value), 0)
    	camera.CFrame = rotatedCFrame:ToWorldSpace(CFrame.new(cameraOffset))
    	if lookAtTarget == true then
    		camera.CFrame = CFrame.new(camera.CFrame.Position, target.Position)
    	end
    end
     
    -- Set up and start rotation tween
    local tweenInfo = TweenInfo.new(rotationTime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, rotationRepeatCount)
    local tween = TweenService:Create(rotationAngle, tweenInfo, {Value=rotationDegrees})
    tween.Completed:Connect(function()
    	tweenComplete = true
    end)
    tween:Play()
     
    -- Update camera position while tween runs
    RunService.RenderStepped:Connect(function()
    	if tweenComplete == false then
    		updateCamera()
    	end
    end)


Should work

3 Likes