3rd Person Camera On a Part

Hello, I need making a script that can manipulate the camera to become 3rd person on a part, I need this because I am trying to make it so the player “becomes” a part and they can move as that part, I don’t really know how to manipulate the camera, so I am looking for some help.

I also need this so if you wait a bit I will make an example.

1 Like

I made this which you can use as a starting point, it goes in StarterPlayerScripts:

--Camera on part script
--This is close to what the default camera does, sorta

--This is to prevent this script running from StarterPlayerScipts
if not script:IsDescendantOf(game:GetService("Players")) then
	return
end

local userInputService = game:GetService("UserInputService")

local function AttachCameraToPart(part : BasePart)	
	--Wait for current camera
	local currentCamera = nil

	repeat
		currentCamera = workspace.CurrentCamera
	until currentCamera ~= nil

	local oldSubject = currentCamera.CameraSubject
	local oldType = currentCamera.CameraType
	
	currentCamera.CameraType = Enum.CameraType.Scriptable
	currentCamera.CameraSubject = part
	
	print("Old type: " ..tostring(oldType))
	print("Old subject: " ..tostring(oldSubject))
	
	--Lock the mouse on right click
	--The default camera does this, we need to do it ourselves

	local event1 = userInputService.InputBegan:Connect(function(inputObject, gameProcessedEvent)
		if inputObject.UserInputType == Enum.UserInputType.MouseButton2 then
			userInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
		end
	end)

	local event2 = userInputService.InputEnded:Connect(function(inputObject, gameProcessedEvent)
		if inputObject.UserInputType == Enum.UserInputType.MouseButton2 then
			userInputService.MouseBehavior = Enum.MouseBehavior.Default
		end
	end)

	--Camera position
	local camYawRad = 0.0
	local camPitchRad = 0.0
	local camDist = 10.0

	--Camera sensitivity
	local camYawSens = -0.015
	local camPitchSens = -0.015
	local camZoomSens = -3.0
	
	--Update the camera CFrame with current values
	local function UpdateCam()
		currentCamera.CFrame = CFrame.fromAxisAngle(Vector3.yAxis, camYawRad) * CFrame.fromAxisAngle(Vector3.xAxis, camPitchRad) * CFrame.new(Vector3.zAxis * camDist) 
		currentCamera.CFrame += part.Position
	end
	
	local event3 = userInputService.InputChanged:Connect(function(inputObject, gameProcessedEvent)
		local needUpdateCam = true

		if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
			--This only gets the delta while the mouse is locked
			--Adjust the camera yaw and pitch

			camYawRad += inputObject.Delta.X * camYawSens
			camPitchRad = math.clamp(camPitchRad + inputObject.Delta.Y * camPitchSens, -math.pi / 2.0, math.pi / 2.0)

		elseif inputObject.UserInputType == Enum.UserInputType.MouseWheel then
			camDist = math.clamp(camDist + inputObject.Position.Z * camZoomSens, 0.0, 100.0)
			--Dumb API decision ^
		else
			needUpdateCam = false
		end

		if needUpdateCam then
			--Update camera position since one of the inputs changed
			UpdateCam()
		end
	end)
	
	--Function to disconnect camera
	local disconnect = function()
		--Reset camera to what it was before
		currentCamera.CameraType = oldType
		currentCamera.CameraSubject = oldSubject
		
		--Disconnect camera controls
		event1:Disconnect()
		event2:Disconnect()
		event3:Disconnect()
		
		--Make sure the mouse isn't stuck locked
		userInputService.MouseBehavior = Enum.MouseBehavior.Default
	end
	
	--Initial camera update
	UpdateCam()
	
	return disconnect
end


--Example usage
wait(5)

local disconnect = AttachCameraToPart(workspace.CamTarget)

wait(5)

--Call the return value to switch back the camera
disconnect()
1 Like

How do I change the part from the spawn location? I set it to the part I wanted but it did not move from the spawn.

1 Like

I fixed a problem in the previous reply. Note that it also only updates the camera when the user moves it, not if the target part moves itself. If you want it to update every frame you can start a RenderStepped connection inside the call and disconnect it in the disconnect function, similar to the others, which calls UpdateCam.

1 Like