local function s(s,P)return math.atan2(P.X*s.Z-P.Z*s.X,P.X*s.X+P.Z*s.Z)end local function P()local P=RootCameraCreator()local m=math.rad(220)local T=math.rad(0)local w=math.rad(250)local r=2 local v=tick()P.LastUserPanCamera=tick()function P.Update(G)local X=tick()local L=X-v local t=G.UserPanningTheCamera==true local u=workspace.CurrentCamera local k=PlayersService.LocalPlayer local x=G:GetHumanoid()local C=u and u.CameraSubject local H=x and x:GetState()==Enum.HumanoidStateType.Climbing local f=C and C:IsA("\086\101\104\105\099\108\101\083\101\097\116")local V=C and C:IsA("\083\107\097\116\101\098\111\097\114\100\080\108\097\116\102\111\114\109")if v==nil or X-v>1 then P:ResetCameraLook()G.LastCameraTransform=nil end if v then if G:ShouldUseVRRotation()then G.RotateInput=G.RotateInput+G:GetVRRotationInput()else local s=math.min(.1,X-v)local P=0 if not(f or V)then P=P+(G.TurningLeft and-120 or 0)P=P+(G.TurningRight and 120 or 0)end local m=G:UpdateGamepad()if m~=Vector2.new(0,0)then t=true G.RotateInput=G.RotateInput+m*s end if P~=0 then t=true G.RotateInput=G.RotateInput+Vector2.new(math.rad(P*s),0)end end end if t then T=0 P.LastUserPanCamera=tick()end local b=X-P.LastUserPanCamera<r local d=G:GetSubjectPosition()if d and(k and u)then local P=G:GetCameraZoom()if P<.5 then P=.5 end if G:GetShiftLock()and not G:IsInFirstPerson()then local s=G:RotateCamera(G:GetCameraLook(),G.RotateInput)local P=((s*XZ_VECTOR):Cross(UP_VECTOR)).unit*1.75 if IsFiniteVector3(P)then d=d+P end else if G.LastCameraTransform and not t then local P=G:IsInFirstPerson()if(H or f or V)and(v and(x and x.Torso))then if P then if G.LastSubjectCFrame and((f or V)and C:IsA("\066\097\115\101\080\097\114\116"))then local P=-s(G.LastSubjectCFrame.lookVector,C.CFrame.lookVector)if IsFinite(P)then G.RotateInput=G.RotateInput+Vector2.new(P,0)end T=0 end elseif not b then local P=x.Torso.CFrame.lookVector if V then P=C.CFrame.lookVector end T=clamp(0,w,T+m*L)local r=clamp(0,1,T*L)if not H and G:IsInFirstPerson()then r=1 end local v=s(P,G:GetCameraLook())if IsFinite(v)and math.abs(v)>.0001 then G.RotateInput=G.RotateInput+Vector2.new(v*r,0)end end elseif not(P or b)and(HasVRAPI and not UserInputService.VREnabled)then local P=-(G.LastCameraTransform.p-d)local m=s(P,G:GetCameraLook())if IsFinite(m)and math.abs(m)>.0001 then G.RotateInput=G.RotateInput+Vector2.new(m,0)end end end end local r=G:RotateCamera(G:GetCameraLook(),G.RotateInput)G.RotateInput=Vector2.new()u.Focus=UserInputService.VREnabled and G:GetVRFocus(d,L)or CFrame.new(d)u.CoordinateFrame=CFrame.new(u.Focus.p-P*r,u.Focus.p)+Vector3.new(0,G:GetCameraHeight(),0)G.LastCameraTransform=u.CoordinateFrame G.LastCameraFocus=u.Focus if f or V and C:IsA("\066\097\115\101\080\097\114\116")then G.LastSubjectCFrame=C.CFrame else G.LastSubjectCFrame=nil end end v=X end return P end return P
-- 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 timeDelta = (now - lastUpdate)

		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
			if self:ShouldUseVRRotation() then
				self.RotateInput = self.RotateInput + self:GetVRRotationInput()
			else
				-- Cap out the delta to 0.1 so we don't get some crazy things when we re-resume from
				local delta = math.min(0.1, now - lastUpdate)
				local angle = 0
				-- NOTE: Traditional follow camera does not rotate with arrow keys
				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 * delta)
				end

				if angle ~= 0 then
					userPanningTheCamera = true
					self.RotateInput = self.RotateInput + Vector2.new(math.rad(angle * delta), 0)
				end
			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

							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)
						and (HasVRAPI and not UserInputService.VREnabled)
					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 = UserInputService.VREnabled and self:GetVRFocus(subjectPosition, timeDelta)
				or CFrame.new(subjectPosition)
			camera.CoordinateFrame = CFrame.new(camera.Focus.p - (zoom * newLookVector), camera.Focus.p)
				+ Vector3.new(0, self:GetCameraHeight(), 0)

			self.LastCameraTransform = camera.CoordinateFrame
			self.LastCameraFocus = camera.Focus
			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
