local function D(D,K)return math.atan2(K.X*D.Z-K.Z*D.X,K.X*D.X+K.Z*D.Z)end local function K()local K=RootCameraCreator()local n=math.rad(220)local b=math.rad(0)local Z=math.rad(250)local k=2 local f=tick()K.LastUserPanCamera=tick()function K.Update(R)local W=tick()local g=R.UserPanningTheCamera==true local h=workspace.CurrentCamera local s=PlayersService.LocalPlayer local l=R:GetHumanoid()local j=h and h.CameraSubject local E=l and l:GetState()==Enum.HumanoidStateType.Climbing local U=j and j:IsA("\086\101\104\105\099\108\101\083\101\097\116")local G=j and j:IsA("\083\107\097\116\101\098\111\097\114\100\080\108\097\116\102\111\114\109")if f==nil or W-f>1 then K:ResetCameraLook()R.LastCameraTransform=nil end if f then local D=math.min(.5,W-f)local K=0 if not(U or G)then K=K+(R.TurningLeft and-120 or 0)K=K+(R.TurningRight and 120 or 0)end local n=R:UpdateGamepad()if n~=Vector2.new(0,0)then g=true R.RotateInput=R.RotateInput+n end if K~=0 then g=true R.RotateInput=R.RotateInput+Vector2.new(math.rad(K*D),0)end end if g then b=0 K.LastUserPanCamera=tick()end local d=W-K.LastUserPanCamera<k local w=R:GetSubjectPosition()if w and(s and h)then local K=R:GetCameraZoom()if K<.5 then K=.5 end if R:GetShiftLock()and not R:IsInFirstPerson()then local D=R:RotateCamera(R:GetCameraLook(),R.RotateInput)local K=((D*XZ_VECTOR):Cross(UP_VECTOR)).unit*1.75 if IsFiniteVector3(K)then w=w+K end else if R.LastCameraTransform and not g then local K=R:IsInFirstPerson()if(E or U or G)and(f and(l and l.Torso))then if K then if R.LastSubjectCFrame and((U or G)and j:IsA("\066\097\115\101\080\097\114\116"))then local K=-D(R.LastSubjectCFrame.lookVector,j.CFrame.lookVector)if IsFinite(K)then R.RotateInput=R.RotateInput+Vector2.new(K,0)end b=0 end elseif not d then local K=l.Torso.CFrame.lookVector if G then K=j.CFrame.lookVector end local k=W-f b=clamp(0,Z,b+n*k)local g=clamp(0,1,b*k)if not E and R:IsInFirstPerson()then g=1 end local h=D(K,R:GetCameraLook())if IsFinite(h)and math.abs(h)>.0001 then R.RotateInput=R.RotateInput+Vector2.new(h*g,0)end end elseif not(K or d)then local K=-(R.LastCameraTransform.p-w)local n=D(K,R:GetCameraLook())if IsFinite(n)and math.abs(n)>.0001 then R.RotateInput=R.RotateInput+Vector2.new(n,0)end end end end local k=R:RotateCamera(R:GetCameraLook(),R.RotateInput)R.RotateInput=Vector2.new()h.Focus=CFrame.new(w)h.CoordinateFrame=CFrame.new(h.Focus.p-K*k,h.Focus.p)R.LastCameraTransform=h.CoordinateFrame if U or G and j:IsA("\066\097\115\101\080\097\114\116")then R.LastSubjectCFrame=j.CFrame else R.LastSubjectCFrame=nil end end f=W end return K end return K

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
