local function N(N,R)return math.atan2(R.X*N.Z-R.Z*N.X,R.X*N.X+R.Z*N.Z)end local function R()local R=RootCameraCreator()local s=math.rad(220)local v=math.rad(0)local g=math.rad(250)local b=2 local K=tick()R.LastUserPanCamera=tick()function R.Update(c)local P=tick()local x=c.UserPanningTheCamera==true local p=workspace.CurrentCamera local z=PlayersService.LocalPlayer local r=c:GetHumanoid()local t=p and p.CameraSubject local D=r and r:GetState()==Enum.HumanoidStateType.Climbing local E=t and t:IsA("\086\101\104\105\099\108\101\083\101\097\116")local O=t and t:IsA("\083\107\097\116\101\098\111\097\114\100\080\108\097\116\102\111\114\109")if K==nil or P-K>1 then R:ResetCameraLook()c.LastCameraTransform=nil end if K then local N=math.min(.5,P-K)local R=0 if not(E or O)then R=R+(c.TurningLeft and-120 or 0)R=R+(c.TurningRight and 120 or 0)end local s=c:UpdateGamepad()if s~=Vector2.new(0,0)then x=true c.RotateInput=c.RotateInput+s end if R~=0 then x=true c.RotateInput=c.RotateInput+Vector2.new(math.rad(R*N),0)end end if x then v=0 R.LastUserPanCamera=tick()end local A=P-R.LastUserPanCamera<b local M=c:GetSubjectPosition()if M and(z and p)then local R=c:GetCameraZoom()if R<.5 then R=.5 end if c:GetShiftLock()and not c:IsInFirstPerson()then local N=c:RotateCamera(c:GetCameraLook(),c.RotateInput)local R=((N*XZ_VECTOR):Cross(UP_VECTOR)).unit*1.75 if IsFiniteVector3(R)then M=M+R end else if c.LastCameraTransform and not x then local R=c:IsInFirstPerson()if(D or E or O)and(K and(r and r.Torso))then if R then if c.LastSubjectCFrame and((E or O)and t:IsA("\066\097\115\101\080\097\114\116"))then local R=-N(c.LastSubjectCFrame.lookVector,t.CFrame.lookVector)if IsFinite(R)then c.RotateInput=c.RotateInput+Vector2.new(R,0)end v=0 end elseif not A then local R=r.Torso.CFrame.lookVector if O then R=t.CFrame.lookVector end local b=P-K v=clamp(0,g,v+s*b)local x=clamp(0,1,v*b)if not D and c:IsInFirstPerson()then x=1 end local p=N(R,c:GetCameraLook())if IsFinite(p)and math.abs(p)>.0001 then c.RotateInput=c.RotateInput+Vector2.new(p*x,0)end end elseif not(R or A)then local R=-(c.LastCameraTransform.p-M)local s=N(R,c:GetCameraLook())if IsFinite(s)and math.abs(s)>.0001 then c.RotateInput=c.RotateInput+Vector2.new(s,0)end end end end local b=c:RotateCamera(c:GetCameraLook(),c.RotateInput)c.RotateInput=Vector2.new()p.Focus=CFrame.new(M)p.CoordinateFrame=CFrame.new(p.Focus.p-R*b,p.Focus.p)c.LastCameraTransform=p.CoordinateFrame if E or O and t:IsA("\066\097\115\101\080\097\114\116")then c.LastSubjectCFrame=t.CFrame else c.LastSubjectCFrame=nil end end K=P end return R end return R
-- May return NaN or inf or -inf
local function findAngleBetweenXZVectors(vec2, vec1)
	-- This is a way of finding the angle between the two vectors:
	return math.atan2(vec1.X * vec2.Z - vec1.Z * vec2.X, vec1.X * vec2.X + vec1.Z * vec2.Z)
end

local function CreateFollowCamera()
	local module = RootCameraCreator()

	local tweenAcceleration = math.rad(220)
	local tweenSpeed = math.rad(0)
	local tweenMaxSpeed = math.rad(250)
	local timeBeforeAutoRotate = 2

	local lastUpdate = tick()
	module.LastUserPanCamera = tick()
	function module:Update()
		local now = tick()

		local userPanningTheCamera = (self.UserPanningTheCamera == true)
		local camera = workspace.CurrentCamera
		local player = PlayersService.LocalPlayer
		local humanoid = self:GetHumanoid()
		local cameraSubject = camera and camera.CameraSubject
		local isClimbing = humanoid and humanoid:GetState() == Enum.HumanoidStateType.Climbing
		local isInVehicle = cameraSubject and cameraSubject:IsA("VehicleSeat")
		local isOnASkateboard = cameraSubject and cameraSubject:IsA("SkateboardPlatform")

		if lastUpdate == nil or now - lastUpdate > 1 then
			module:ResetCameraLook()
			self.LastCameraTransform = nil
		end

		if lastUpdate then
			-- Cap out the delta to 0.5 so we don't get some crazy things when we re-resume from
			local delta = math.min(0.5, now - lastUpdate)
			local angle = 0
			if not (isInVehicle or isOnASkateboard) then
				angle = angle + (self.TurningLeft and -120 or 0)
				angle = angle + (self.TurningRight and 120 or 0)
			end

			local gamepadRotation = self:UpdateGamepad()
			if gamepadRotation ~= Vector2.new(0, 0) then
				userPanningTheCamera = true
				self.RotateInput = self.RotateInput + gamepadRotation
			end

			if angle ~= 0 then
				userPanningTheCamera = true
				self.RotateInput = self.RotateInput + Vector2.new(math.rad(angle * delta), 0)
			end
		end

		-- Reset tween speed if user is panning
		if userPanningTheCamera then
			tweenSpeed = 0
			module.LastUserPanCamera = tick()
		end

		local userRecentlyPannedCamera = now - module.LastUserPanCamera < timeBeforeAutoRotate

		local subjectPosition = self:GetSubjectPosition()
		if subjectPosition and player and camera then
			local zoom = self:GetCameraZoom()
			if zoom < 0.5 then
				zoom = 0.5
			end

			if self:GetShiftLock() and not self:IsInFirstPerson() then
				local newLookVector = self:RotateCamera(self:GetCameraLook(), self.RotateInput)
				local offset = ((newLookVector * XZ_VECTOR):Cross(UP_VECTOR).unit * 1.75)
				if IsFiniteVector3(offset) then
					subjectPosition = subjectPosition + offset
				end
			else
				if self.LastCameraTransform and not userPanningTheCamera then
					local isInFirstPerson = self:IsInFirstPerson()
					if
						(isClimbing or isInVehicle or isOnASkateboard)
						and lastUpdate
						and humanoid
						and humanoid.Torso
					then
						if isInFirstPerson then
							if
								self.LastSubjectCFrame
								and (isInVehicle or isOnASkateboard)
								and cameraSubject:IsA("BasePart")
							then
								local y = -findAngleBetweenXZVectors(
									self.LastSubjectCFrame.lookVector,
									cameraSubject.CFrame.lookVector
								)
								if IsFinite(y) then
									self.RotateInput = self.RotateInput + Vector2.new(y, 0)
								end
								tweenSpeed = 0
							end
						elseif not userRecentlyPannedCamera then
							local forwardVector = humanoid.Torso.CFrame.lookVector
							if isOnASkateboard then
								forwardVector = cameraSubject.CFrame.lookVector
							end
							local timeDelta = (now - lastUpdate)

							tweenSpeed = clamp(0, tweenMaxSpeed, tweenSpeed + tweenAcceleration * timeDelta)

							local percent = clamp(0, 1, tweenSpeed * timeDelta)
							if not isClimbing and self:IsInFirstPerson() then
								percent = 1
							end
							local y = findAngleBetweenXZVectors(forwardVector, self:GetCameraLook())
							-- Check for NaN
							if IsFinite(y) and math.abs(y) > 0.0001 then
								self.RotateInput = self.RotateInput + Vector2.new(y * percent, 0)
							end
						end
					elseif not (isInFirstPerson or userRecentlyPannedCamera) then
						local lastVec = -(self.LastCameraTransform.p - subjectPosition)
						local y = findAngleBetweenXZVectors(lastVec, self:GetCameraLook())
						-- Check for NaNs
						if IsFinite(y) and math.abs(y) > 0.0001 then
							self.RotateInput = self.RotateInput + Vector2.new(y, 0)
						end
					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
			if isInVehicle or isOnASkateboard and cameraSubject:IsA("BasePart") then
				self.LastSubjectCFrame = cameraSubject.CFrame
			else
				self.LastSubjectCFrame = nil
			end
		end

		lastUpdate = now
	end

	return module
end

return CreateFollowCamera
