local function a(a,Q)return math.atan2(Q.X*a.Z-Q.Z*a.X,Q.X*a.X+Q.Z*a.Z)end local function Q()local Q=RootCameraCreator()local d=math.rad(220)local B=math.rad(0)local c=math.rad(250)local i=2 local G=nil local y=.7 local r=0 local W=.1 local z=1 local h=Vector2.new(0,0)local L=.8 local J=nil local H=tick()Q.LastUserPanCamera=tick()function Q.Update(G)local y=tick()local r=G.UserPanningTheCamera==true local W=workspace.CurrentCamera local z=PlayersService.LocalPlayer local h=G:GetHumanoid()local L=W and W.CameraSubject local J=L and L:IsA("\086\101\104\105\099\108\101\083\101\097\116")local b=L and L:IsA("\083\107\097\116\101\098\111\097\114\100\080\108\097\116\102\111\114\109")if H==nil or y-H>1 then Q:ResetCameraLook()G.LastCameraTransform=nil end if H then local a=math.min(.5,y-H)local Q=0 if not(J or b)then Q=Q+(G.TurningLeft and-120 or 0)Q=Q+(G.TurningRight and 120 or 0)end local d=G:UpdateGamepad()if d~=Vector2.new(0,0)then r=true G.RotateInput=G.RotateInput+d end if Q~=0 then r=true G.RotateInput=G.RotateInput+Vector2.new(math.rad(Q*a),0)end end if r then B=0 Q.LastUserPanCamera=tick()end local E=y-Q.LastUserPanCamera<i local e=G:GetSubjectPosition()if e and(z and W)then local Q=G:GetCameraZoom()if Q<.5 then Q=.5 end if G:GetShiftLock()and not G:IsInFirstPerson()then local a=G:RotateCamera(G:GetCameraLook(),G.RotateInput)local Q=((a*XZ_VECTOR):Cross(UP_VECTOR)).unit*1.75 if IsFiniteVector3(Q)then e=e+Q end else if G.LastCameraTransform and not r then local Q=G:IsInFirstPerson()if(J or b)and(H and(h and h.Torso))then if Q then if G.LastSubjectCFrame and((J or b)and L:IsA("\066\097\115\101\080\097\114\116"))then local Q=-a(G.LastSubjectCFrame.lookVector,L.CFrame.lookVector)if IsFinite(Q)then G.RotateInput=G.RotateInput+Vector2.new(Q,0)end B=0 end elseif not E then local Q=h.Torso.CFrame.lookVector if b then Q=L.CFrame.lookVector end local i=y-H B=clamp(0,c,B+d*i)local r=clamp(0,1,B*i)if G:IsInFirstPerson()then r=1 end local W=a(Q,G:GetCameraLook())if IsFinite(W)and math.abs(W)>.0001 then G.RotateInput=G.RotateInput+Vector2.new(W*r,0)end end end end end local i=G:RotateCamera(G:GetCameraLook(),G.RotateInput)G.RotateInput=Vector2.new()W.Focus=CFrame.new(e)W.CoordinateFrame=CFrame.new(W.Focus.p-Q*i,W.Focus.p)G.LastCameraTransform=W.CoordinateFrame if J or b and L:IsA("\066\097\115\101\080\097\114\116")then G.LastSubjectCFrame=L.CFrame else G.LastSubjectCFrame=nil end end H=y end return Q end return Q
-- May return NaN or inf or -inf
-- This is a way of finding the angle between the two vectors:
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 CreateClassicCamera()
	local module = RootCameraCreator()

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

	local lastThumbstickRotate = nil
	local numOfSeconds = 0.7
	local currentSpeed = 0
	local maxSpeed = 0.1
	local thumbstickSensitivity = 1
	local lastThumbstickPos = Vector2.new(0, 0)
	local ySensitivity = 0.8
	local lastVelocity = nil

	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 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
				-- We need to use the right vector of the camera after rotation, not before
				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 (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 self:IsInFirstPerson() then
								percent = 1
							end

							local y = findAngleBetweenXZVectors(forwardVector, self:GetCameraLook())
							if IsFinite(y) and math.abs(y) > 0.0001 then
								self.RotateInput = self.RotateInput + Vector2.new(y * percent, 0)
							end
						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 CreateClassicCamera
