Custom center-locked mouse camera control toggle


#1

I’ve read a bit of the new camera and character control modules source code and came up with an implementation for center-locking the mouse while controlling the camera that also supports shift lock.

Shift lock has properties for developers to disable it in their games, but those properties are read-only in runtime, so you can’t use them to disable shift lock temporarily to stop it from affecting the camera. I’ve found a loophole, though: a StringValue named BoundKeys is available to change which keys will activate shift lock if it is enabled. Setting the value to an empty string disables the toggle.

The function SetIsMouseLocked in the CameraController module behaves just like shift lock. The mouse gets center-locked, moving it moves the camera, and the character faces in the direction the camera faces. SetMouseLockOffset sets the camera offset from the center; shift lock uses 1.75, 0, 0.

local PlayerModule = game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule")
local cameras = require(PlayerModule):GetCameras()
local BoundKeys = PlayerModule.CameraModule.MouseLockController:WaitForChild("BoundKeys")
local OldBoundKeys = BoundKeys.Value

local CenterLocked = false
local function ToggleLock()
	local CameraController = cameras.activeCameraController
	local MouseLockController = cameras.activeMouseLockController
	CenterLocked = not CenterLocked
	if CenterLocked then
		if MouseLockController:GetIsMouseLocked() then -- toggle shift lock off
			MouseLockController:OnMouseLockToggled()
		end
		CameraController:SetMouseLockOffset(Vector3.new())
		CameraController:SetIsMouseLocked(true)
		BoundKeys.Value = "" -- disables shift lock toggle
	else
		CameraController:SetIsMouseLocked(false)
		BoundKeys.Value = OldBoundKeys -- restores shift lock toggle
	end
end

-- example use, press T to toggle center lock
game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode.T then
		ToggleLock()
	end
end)