local function b(b,r)return math.atan2(r.X*b.Z-r.Z*b.X,r.X*b.X+r.Z*b.Z)end local function r()local r=RootCameraCreator()local y=tick()function r.Update(X)local l=tick()local n=workspace.CurrentCamera local T=PlayersService.LocalPlayer if y==nil or l-y>1 then r:ResetCameraLook()X.LastCameraTransform=nil end local N=X:GetSubjectPosition()if N and(T and n)then local r=X:GetCameraZoom()if r<=0 then r=.1 end if X.LastCameraTransform then local r=X:GetHumanoid()if y and(r and r.Torso)then local y=r.Torso.CFrame.lookVector local l=b(y,X:GetCameraLook())if IsFinite(l)and math.abs(l)>.0001 then X.RotateInput=X.RotateInput+Vector2.new(l,0)end end end local l=X:RotateCamera(X:GetCameraLook(),X.RotateInput)X.RotateInput=Vector2.new()n.Focus=CFrame.new(N)n.CoordinateFrame=CFrame.new(n.Focus.p-r*l,n.Focus.p)X.LastCameraTransform=n.CoordinateFrame end y=l end return r end return r

local function findAngleBetweenXZVectors(vec2, vec1)
	return math.atan2(vec1.X*vec2.Z-vec1.Z*vec2.X, vec1.X*vec2.X + vec1.Z*vec2.Z)
end

local function CreateAttachCamera()
	local module = RootCameraCreator()
	
	local lastUpdate = tick()
	function module:Update()
		local now = tick()
		
		local camera = 	workspace.CurrentCamera
		local player = PlayersService.LocalPlayer
		
		if lastUpdate == nil or now - lastUpdate > 1 then
			module:ResetCameraLook()
			self.LastCameraTransform = nil
		end	
		
		local subjectPosition = self:GetSubjectPosition()		
		if subjectPosition and player and camera then
			local zoom = self:GetCameraZoom()
			if zoom <= 0 then
				zoom = 0.1
			end
			
			if self.LastCameraTransform then
				local humanoid = self:GetHumanoid()
				if lastUpdate and humanoid and humanoid.Torso then
					local forwardVector = humanoid.Torso.CFrame.lookVector

					local y = findAngleBetweenXZVectors(forwardVector, self:GetCameraLook())
					if IsFinite(y) and math.abs(y) > 0.0001 then
						self.RotateInput = self.RotateInput + Vector2.new(y, 0)
					end
				end
			end
			local newLookVector = self:RotateCamera(self:GetCameraLook(), self.RotateInput)
			self.RotateInput = Vector2.new()
			
			camera.Focus = CFrame.new(subjectPosition)
			camera.CoordinateFrame = CFrame.new(camera.Focus.p - (zoom * newLookVector), camera.Focus.p)
			self.LastCameraTransform = camera.CoordinateFrame
		end
		lastUpdate = now
	end
	
	return module
end

return CreateAttachCamera
