Unlocked mouse does not start in center of screen

What do you want to achieve?
I have a mechanic in my game where you can place markers, but the player needs to select a color. similar to Valorant’s marker placement system. Everything works in the placement system.

What is the issue?:
When the script unlocks the mouse, (using MouseBehavior.Default and MouseBehavior.LockCenter) whenever the mouse is unlocked, no matter what I do the mouse always unlocks somewhere in the bottom left of the screen. And makes the whole system very difficult to use.

What solutions have you tried so far?:
As far as I know, I haven’t seen anything about this issue on the DevForum, I think I’m just missing something. To unlock the mouse, I’m using a built-in feature in a camera script I found by WhoBloxxedWho. It uses those lines mentioned above to unlock the mouse. I’m using a global variable in Replicated Storage that the Camera script checks for the state of the mouse, so I can unlock the mouse from menu scripts and whatnot. Here’s the camera script:

---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- 
-- StarterGui -- 					
--	--	--	--	--	--	--	--	--	--	--	--	--	--	--	--	--	--	--
--	Place this into StarterGui or StarterPack							--
--	CREDIT																--
--	WhoBloxxedWho; for the initial script								--
--	DoogleFox; some stuff ripped from his panner script					--
--	DuruTeru; shoving it all into this script and then some :)			--
--	UPDATE: turned it into r15, made it so you can swim right in water	--
--  Jan 07, 2017														--
--  UPDATE: added walkspeed easing directionally, also adding running   --	
--  Nov 13, 2020														--
--	--	--	--	--	--	--	--	--	--	--	--	--	--	--	--	--	--	--


---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- 


repeat wait() until game:GetService("Players").LocalPlayer.Character ~= nil
local runService = game:GetService("RunService")
local input = game:GetService("UserInputService")
local reps = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local settings = reps.Settings

-- you can mess with these settings
CanToggleMouse = {allowed = true; activationkey = nil;} -- lets you move your mouse around in firstperson
CanViewBody = settings.canseeself.Value	-- whether you see your body
Sensitivity = settings.sensitivity.Value		-- anything higher would make looking up and down harder; recommend anything between 0~1
Smoothness = 0.05		-- recommend anything between 0~1
FieldOfView = 60		-- fov
HeadOffset = CFrame.new(0,0.7,0) -- how far your camera is from your head

local cam = game.Workspace.CurrentCamera
local player = players.LocalPlayer
local m = player:GetMouse()
m.Icon = "http://www.roblox.com/asset/?id=569021388" -- replaces mouse icon
local character = player.Character or player.CharacterAdded:wait()
local human = character.Humanoid
local humanoidpart = character.HumanoidRootPart

local head = character:WaitForChild("Head")
local CamPos,TargetCamPos = cam.CoordinateFrame.p,cam.CoordinateFrame.p 
local AngleX,TargetAngleX = 0,0
local AngleY,TargetAngleY = 0,0

local running = false
local freemouse = settings.MouseUnlocked.Value
local defFOV = FieldOfView

local w, a, s, d, lshift = false, false, false, false, false

-- you can mess with these settings
local easingtime = 0.5 --0~1
local walkspeeds = {
	enabled =		  true;
	walkingspeed =		5;
	backwardsspeed =	2;
	sidewaysspeed =		3;
	diagonalspeed =		3;
	runningspeed =		5;
	runningFOV=			60;
}

---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- 

local function getScreenCenter()
	local viewportSize = cam.ViewportSize
	local centerX = viewportSize.X / 2
	local centerY = viewportSize.Y / 2
	return centerX, centerY
end

local function moveToCenter()
	-- Calculate the center of the screen
	local centerX, centerY = getScreenCenter()
end

function updatechar()

	for _, v in pairs(character:GetChildren())do
		if CanViewBody then
			if v.Name == 'Head' then
				v.LocalTransparencyModifier = 1
				v.CanCollide = false
				v.face.LocalTransparencyModifier = 1
			end
		else
			if v:IsA'Part' or v:IsA'UnionOperation' or v:IsA'MeshPart' then
				v.LocalTransparencyModifier = 1
				v.CanCollide = true
			end
		end
		if v:IsA'Accessory' then
			v:FindFirstChild('Handle').LocalTransparencyModifier = 1
			v:FindFirstChild('Handle').CanCollide = false
		end
		if v:IsA'Hat' then
			v:FindFirstChild('Handle').LocalTransparencyModifier = 1
			v:FindFirstChild('Handle').CanCollide = false
		end

	end

end

-- math, thx roblox wiki
function lerp(a, b, t)
	return a * (1-t) + (b*t)
end


---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- 

input.InputChanged:connect(function(inputObject)

	if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
		local delta = Vector2.new(inputObject.Delta.x/Sensitivity,inputObject.Delta.y/Sensitivity) * Smoothness

		local X = TargetAngleX - delta.y 
		TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X 
		TargetAngleY = (TargetAngleY - delta.x) %360 
	end	

end)

input.InputBegan:connect(function(inputObject)

	if inputObject.UserInputType == Enum.UserInputType.Keyboard then
		if inputObject.KeyCode == CanToggleMouse.activationkey then
			if CanToggleMouse.allowed and freemouse == false then
				freemouse = true
			else
				freemouse = false
			end
		end
	end

	if inputObject.UserInputType == Enum.UserInputType.Keyboard then
		if inputObject.KeyCode == Enum.KeyCode.W then
			w = true
		end

		if inputObject.KeyCode == Enum.KeyCode.A then
			a = true
		end

		if inputObject.KeyCode == Enum.KeyCode.S then
			s = true
		end

		if inputObject.KeyCode == Enum.KeyCode.D then
			d = true
		end

		if inputObject.KeyCode == Enum.KeyCode.LeftShift then
			lshift = true
		end
	end
end)

input.InputEnded:connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.Keyboard then
		if inputObject.KeyCode == Enum.KeyCode.W then
			w = false
		end

		if inputObject.KeyCode == Enum.KeyCode.A then
			a = false
		end

		if inputObject.KeyCode == Enum.KeyCode.S then
			s = false
		end

		if inputObject.KeyCode == Enum.KeyCode.D then
			d = false
		end

		if inputObject.KeyCode == Enum.KeyCode.LeftShift then
			lshift = false
		end
	end
end)

---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- 

runService.RenderStepped:connect(function()
	CanViewBody = settings.canseeself.Value	-- whether you see your body
	Sensitivity = settings.sensitivity.Value		-- anything higher would make looking up and down harder; recommend anything between 0~1
	local freemouse = settings.MouseUnlocked.Value
	local stopMovementFlag = character:FindFirstChild("StopMovement")
	if stopMovementFlag and stopMovementFlag.Value then
		return
	end

	if running then
		updatechar()

		CamPos = CamPos + (TargetCamPos - CamPos) *0.28 
		AngleX = AngleX + (TargetAngleX - AngleX) *0.35 
		local dist = TargetAngleY - AngleY 
		dist = math.abs(dist) > 180 and dist - (dist / math.abs(dist)) * 360 or dist 
		AngleY = (AngleY + dist *0.35) %360
		cam.CameraType = Enum.CameraType.Scriptable

		cam.CoordinateFrame = CFrame.new(head.Position) 
			* CFrame.Angles(0,math.rad(AngleY),0) 
			* CFrame.Angles(math.rad(AngleX),0,0)
			* HeadOffset -- offset

		humanoidpart.CFrame=CFrame.new(humanoidpart.Position)*CFrame.Angles(0,math.rad(AngleY),0)
	else game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
	end

	if (cam.Focus.p-cam.CoordinateFrame.p).magnitude < 1 then
		running = false
	else
		running = true
		if freemouse == true then
			game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
			moveToCenter()
		else
			game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
		end
	end

	if not CanToggleMouse.allowed then
		freemouse = false
	end

	cam.FieldOfView = FieldOfView

	if walkspeeds.enabled then
		if w and s then return end

		if w and not lshift then
			FieldOfView = lerp(FieldOfView, defFOV,easingtime)
			human.WalkSpeed = lerp(human.WalkSpeed,walkspeeds.walkingspeed,easingtime)
		elseif w and a then
			human.WalkSpeed = lerp(human.WalkSpeed,walkspeeds.diagonalspeed,easingtime)
		elseif w and d then
			human.WalkSpeed = lerp(human.WalkSpeed,walkspeeds.diagonalspeed,easingtime)
		elseif s then
			human.WalkSpeed = lerp(human.WalkSpeed,walkspeeds.backwardsspeed,easingtime)
		elseif s and a then
			human.WalkSpeed = lerp(human.WalkSpeed,walkspeeds.backwardsspeed - (walkspeeds.diagonalspeed - walkspeeds.backwardsspeed),easingtime)
		elseif s and d then
			human.WalkSpeed = lerp(human.WalkSpeed,walkspeeds.backwardsspeed - (walkspeeds.diagonalspeed - walkspeeds.backwardsspeed),easingtime)
		elseif d then
			human.WalkSpeed = lerp(human.WalkSpeed,walkspeeds.sidewaysspeed,easingtime)
		elseif a then
			human.WalkSpeed = lerp(human.WalkSpeed,walkspeeds.sidewaysspeed,easingtime)
		end	

		if lshift and w then
			FieldOfView = lerp(FieldOfView, walkspeeds.runningFOV,easingtime)
			human.WalkSpeed = lerp(human.WalkSpeed,human.WalkSpeed + (walkspeeds.runningspeed - human.WalkSpeed),easingtime)
		end
	end

end)

---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- 

Thank you to anyone who replies!

If it is always unlocking in the bottom of the screen, that is more than likely an issue with your Roblox Studio. Upload your game and play it, see if the error is still caused.

It is not possible to set the exact position of the mouse in roblox studio using lua. That is an internal thing

thank you! No idea why I didn’t think about checking that, thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.