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

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)
			if _G.LockTarget then
				camera.CoordinateFrame = CFrame.new(camera.Focus.p - (zoom * newLookVector),_G.LockTarget.Torso.Position);
			else
				camera.CoordinateFrame = CFrame.new(camera.Focus.p - (zoom * newLookVector), camera.Focus.p)
			end;
			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
