How do you lock the mouse's position when the user's right mouse button is down?

Hi,

I’m working on making a custom build mode camera controller which is generally going well, however I was wondering how I could go about restoring the right click to lock cursor position feature that exists by default?

My camera’s CameraType is Scriptable and I believe this removed the mouse locking ability.

I don’t really know what else to put but yeah.

Add a LocalScript into StarterPlayer then write this code inside:

local function FocusControl(Name, State, Object)
	if State == Enum.UserInputState.Begin then
		Camera.CameraType = Enum.CameraType.Scriptable
		
		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		UserInputService.MouseIconEnabled = true
		
		ContextActionService:UnbindAction("FocusControl")
	end
end

ContextActionService:BindAction("FocusControl", FocusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.MouseButton2, Enum.UserInputType.Focus)
1 Like

Just logged off for the night, I’ll try this tomorrow. Thanks!

1 Like

This seems to be closer, however it locks the cursor to the centre of the screen instead of where the player puts their cursor.

Well apparently it’s not possible with the actual mouse, look at the solution for this for more ideas maybe:

I saw that but it’s not really what I’m looking for. Roblox has this behaviour already when the camera’s CameraType is “Custom”, try right clicking when you’re in third person view to get an idea of what I’m looking for.

I believe Enum.MouseBehavior.LockCurrentPosition solves your problem.

1 Like
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local uis = game:GetService("UserInputService")

mouse.Button2Down:Connect(function()
	uis.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
end)
mouse.Button2Up:Connect(function()
	uis.MouseBehavior = Enum.MouseBehavior.Default
end)