function OrbitalCamera.GetCameraToSubjectDistance(e)return e.curDistance end function OrbitalCamera.SetCameraToSubjectDistance(u,e)print("\079\114\098\105\116\097\108\067\097\109\101\114\097 \083\101\116\067\097\109\101\114\097\084\111\083\117\098\106\101\099\116\068\105\115\116\097\110\099\101 ",e)local R=PlayersService.LocalPlayer if R then u.currentSubjectDistance=Util.Clamp(u.minDistance,u.maxDistance,e)u.currentSubjectDistance=math.max(u.currentSubjectDistance,u.FIRST_PERSON_DISTANCE_THRESHOLD)end u.inFirstPerson=false u:UpdateMouseBehavior()return u.currentSubjectDistance end function OrbitalCamera.CalculateNewLookVector(R,e,u)local p=e or R:GetCameraLookVector()local j=math.asin(p.y)local q=Util.Clamp(j-math.rad(MAX_ALLOWED_ELEVATION_DEG),j-math.rad(MIN_ALLOWED_ELEVATION_DEG),u.y)local G=Vector2.new(u.x,q)local k=CFrame.new(ZERO_VECTOR3,p)local y=((CFrame.Angles(0,-G.x,0)*k)*CFrame.Angles(-G.y,0,0)).lookVector return y end function OrbitalCamera.GetGamepadPan(p,e,u,R)if R.UserInputType==p.activeGamepad and R.KeyCode==Enum.KeyCode.Thumbstick2 then if p.r3ButtonDown or p.l3ButtonDown then if R.Position.Y>THUMBSTICK_DEADZONE then p.gamepadDollySpeedMultiplier=.96 elseif R.Position.Y<-THUMBSTICK_DEADZONE then p.gamepadDollySpeedMultiplier=1.04 else p.gamepadDollySpeedMultiplier=1.0 end else if u==Enum.UserInputState.Cancel then p.gamepadPanningCamera=ZERO_VECTOR2 return end local e=Vector2.new(R.Position.X,-R.Position.Y)if e.magnitude>THUMBSTICK_DEADZONE then p.gamepadPanningCamera=Vector2.new(R.Position.X,-R.Position.Y)else p.gamepadPanningCamera=ZERO_VECTOR2 end end if FFlagPlayerScriptsBindAtPriority then return Enum.ContextActionResult.Sink end end if FFlagPlayerScriptsBindAtPriority then return Enum.ContextActionResult.Pass end end function OrbitalCamera.DoGamepadZoom(p,e,u,R)if R.UserInputType==p.activeGamepad and(R.KeyCode==Enum.KeyCode.ButtonR3 or R.KeyCode==Enum.KeyCode.ButtonL3)then if u==Enum.UserInputState.Begin then p.r3ButtonDown=R.KeyCode==Enum.KeyCode.ButtonR3 p.l3ButtonDown=R.KeyCode==Enum.KeyCode.ButtonL3 elseif u==Enum.UserInputState.End then if R.KeyCode==Enum.KeyCode.ButtonR3 then p.r3ButtonDown=false elseif R.KeyCode==Enum.KeyCode.ButtonL3 then p.l3ButtonDown=false end if not p.r3ButtonDown and not p.l3ButtonDown then p.gamepadDollySpeedMultiplier=1.0 end end if FFlagPlayerScriptsBindAtPriority then return Enum.ContextActionResult.Sink end end if FFlagPlayerScriptsBindAtPriority then return Enum.ContextActionResult.Pass end end function OrbitalCamera.BindGamepadInputActions(e)if FFlagPlayerScriptsBindAtPriority then e:BindAction("\079\114\098\105\116\097\108\067\097\109\071\097\109\101\112\097\100\080\097\110",function(u,R,p)e:GetGamepadPan(u,R,p)end,false,Enum.KeyCode.Thumbstick2)e:BindAction("\079\114\098\105\116\097\108\067\097\109\071\097\109\101\112\097\100\090\111\111\109",function(u,R,p)e:DoGamepadZoom(u,R,p)end,false,Enum.KeyCode.ButtonR3,Enum.KeyCode.ButtonL3)else local u=game:GetService("\067\111\110\116\101\120\116\065\099\116\105\111\110\083\101\114\118\105\099\101")u:BindAction("\079\114\098\105\116\097\108\067\097\109\071\097\109\101\112\097\100\080\097\110",function(u,R,p)e:GetGamepadPan(u,R,p)end,false,Enum.KeyCode.Thumbstick2)u:BindAction("\079\114\098\105\116\097\108\067\097\109\071\097\109\101\112\097\100\090\111\111\109",function(u,R,p)e:DoGamepadZoom(u,R,p)end,false,Enum.KeyCode.ButtonR3)u:BindAction("\079\114\098\105\116\097\108\067\097\109\071\097\109\101\112\097\100\090\111\111\109\065\108\116",function(u,R,p)e:DoGamepadZoom(u,R,p)end,false,Enum.KeyCode.ButtonL3)end end
--
function OrbitalCamera:GetCameraToSubjectDistance()
	return self.curDistance
end
	
function OrbitalCamera:SetCameraToSubjectDistance(desiredSubjectDistance)
	print("OrbitalCamera SetCameraToSubjectDistance ",desiredSubjectDistance)
	local player = PlayersService.LocalPlayer
	if player then
		self.currentSubjectDistance = Util.Clamp(self.minDistance, self.maxDistance, desiredSubjectDistance)
		
		-- OrbitalCamera is not allowed to go into the first-person range
		self.currentSubjectDistance = math.max(self.currentSubjectDistance, self.FIRST_PERSON_DISTANCE_THRESHOLD)
	end
	self.inFirstPerson = false
	self:UpdateMouseBehavior()
	return self.currentSubjectDistance
end

function OrbitalCamera:CalculateNewLookVector(suppliedLookVector, xyRotateVector)
	local currLookVector = suppliedLookVector or self:GetCameraLookVector()
	local currPitchAngle = math.asin(currLookVector.y)
	local yTheta = Util.Clamp(currPitchAngle - math.rad(MAX_ALLOWED_ELEVATION_DEG), currPitchAngle - math.rad(MIN_ALLOWED_ELEVATION_DEG), xyRotateVector.y)
	local constrainedRotateInput = Vector2.new(xyRotateVector.x, yTheta)	
	local startCFrame = CFrame.new(ZERO_VECTOR3, currLookVector)
	local newLookVector = (CFrame.Angles(0, -constrainedRotateInput.x, 0) * startCFrame * CFrame.Angles(-constrainedRotateInput.y,0,0)).lookVector	
	return newLookVector
end

function OrbitalCamera:GetGamepadPan(name, state, input)
	if input.UserInputType == self.activeGamepad and input.KeyCode == Enum.KeyCode.Thumbstick2 then
		if self.r3ButtonDown or self.l3ButtonDown then
		-- R3 or L3 Thumbstick is depressed, right stick controls dolly in/out
			if (input.Position.Y > THUMBSTICK_DEADZONE) then
				self.gamepadDollySpeedMultiplier = 0.96
			elseif (input.Position.Y < -THUMBSTICK_DEADZONE) then
				self.gamepadDollySpeedMultiplier = 1.04
			else 
				self.gamepadDollySpeedMultiplier = 1.00
			end
		else
			if state == Enum.UserInputState.Cancel then
				self.gamepadPanningCamera = ZERO_VECTOR2
				return
			end		
			
			local inputVector = Vector2.new(input.Position.X, -input.Position.Y)
			if inputVector.magnitude > THUMBSTICK_DEADZONE then
				self.gamepadPanningCamera = Vector2.new(input.Position.X, -input.Position.Y)
			else
				self.gamepadPanningCamera = ZERO_VECTOR2
			end
		end
		if FFlagPlayerScriptsBindAtPriority then
			return Enum.ContextActionResult.Sink
		end
	end
	if FFlagPlayerScriptsBindAtPriority then
		return Enum.ContextActionResult.Pass
	end
end

function OrbitalCamera:DoGamepadZoom(name, state, input)
	if input.UserInputType == self.activeGamepad and (input.KeyCode == Enum.KeyCode.ButtonR3 or input.KeyCode == Enum.KeyCode.ButtonL3) then
		if (state == Enum.UserInputState.Begin) then
			self.r3ButtonDown = input.KeyCode == Enum.KeyCode.ButtonR3
			self.l3ButtonDown = input.KeyCode == Enum.KeyCode.ButtonL3
		elseif (state == Enum.UserInputState.End) then
			if (input.KeyCode == Enum.KeyCode.ButtonR3) then
				self.r3ButtonDown = false
			elseif (input.KeyCode == Enum.KeyCode.ButtonL3) then
				self.l3ButtonDown = false
			end
			if (not self.r3ButtonDown) and (not self.l3ButtonDown) then
				self.gamepadDollySpeedMultiplier = 1.00
			end
		end
		if FFlagPlayerScriptsBindAtPriority then
			return Enum.ContextActionResult.Sink
		end
	end
	if FFlagPlayerScriptsBindAtPriority then
		return Enum.ContextActionResult.Pass
	end
end

function OrbitalCamera:BindGamepadInputActions()
	if FFlagPlayerScriptsBindAtPriority then
		self:BindAction("OrbitalCamGamepadPan", function(name, state, input) self:GetGamepadPan(name, state, input) end,
			false, Enum.KeyCode.Thumbstick2)
		self:BindAction("OrbitalCamGamepadZoom", function(name, state, input) self:DoGamepadZoom(name, state, input) end,
			false, Enum.KeyCode.ButtonR3, Enum.KeyCode.ButtonL3)
	else
		local ContextActionService = game:GetService('ContextActionService')
		
		ContextActionService:BindAction("OrbitalCamGamepadPan", function(name, state, input) self:GetGamepadPan(name, state, input) end, false, Enum.KeyCode.Thumbstick2)	
		ContextActionService:BindAction("OrbitalCamGamepadZoom", function(name, state, input) self:DoGamepadZoom(name, state, input) end, false, Enum.KeyCode.ButtonR3)
		ContextActionService:BindAction("OrbitalCamGamepadZoomAlt", function(name, state, input) self:DoGamepadZoom(name, state, input) end, false, Enum.KeyCode.ButtonL3)
	end
end

