Attached camera that follows every movement of part

I’ve been attempting to create a camera system that will follow every aspect of a part, its position and rotation. My goal is to make it so that no matter where the part is in the world or how it is rotated, it will stay in the same position on the screen.

Here’s the code I’ve come up with:

function updateCamera()
	local charVector = char.Position
	local relativeCFrame = char.CFrame:ToWorldSpace(offsetCFrame) -- offsets the camera based on rotation of the character

	workspace.CurrentCamera.CFrame = CFrame.lookAt(relativeCFrame.p, charVector)
	workspace.CurrentCamera:SetRoll(char.Orientation.x*-1) --  applies a roll effect
end

This works for the most part except in some weird situations. For example, when the parts orientation is (90, 0, 0) (which should turn the camera -90 degrees on the Z axis), I get this mess:


As you can see, for some reason the camera is turning just a bit too much towards the ground.

There are many other examples of weird stuff happening when I use this method, but I’m much more interested in learning why it is happening and all and how I can fix it.

1 Like

I’m trying to do the same. What I’m trying to do is that it works like CameraType.Attach except it doesn’t let the player look up and down and follow every angle of the CameraSubject.

I think this is what you’re looking for? :

local Run = game:GetService("RunService")

local camera = workspace.CurrentCamera
local basePart = workspace.Part

local offset = CFrame.new(0, 1, 5)

Run:BindToRenderStep("Attach", Enum.RenderPriority.Camera.Value, function()
	camera.CFrame = workspace.Part.CFrame * offset
end)
1 Like

I’ll try it to see if it fits what I’m trying to do. Thanks!