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

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
