How do nudge the players mouse towards the center of the screen?

Howdy! Salutations! I’m working on a Hellmet-like game, and so far, I have the camera down. One small issue, however; I want the mouse of the player to slightly be nudged towards the center of the screen. The farther the mouse is from the center, the more it’ll be dragged towards the center, and the more the player has to move their mouse. Any help would be greatly appreciated!

I would provide a coding snippet, but I have NO idea of how to go about doing this.

Cheers!

Their is no way to control the players mouse

not sure if this has script permissions, but you can still try it

VirtualInputManager | Roblox API Reference

You can create a fake mouse then move it based on the real mouse’s movements. This way you have full control over the fake mouse.
By recentering the real mouse you don’t have to worry about it hitting the Roblox window’s borders and thus stopping the fake mouse:

However when using a fake mouse you have to script all mouse based interactions, for example with UI, since the real mouse won’t be at the same spot as the fake mouse.

The real mouse can alternatively be locked into the center of the screen and you can just use UserInputService:GetMouseDelta() to move the fake mouse rather than use this hacky modal recentering method.

Place file: FakeMouse_Test1.rbxl (59.5 KB)

2 Likes

Thank you for this. I will try it out, and update you if I can get it to work! Cheers.

1 Like

For some reason, the mouse keeps moving to the bottom of the screen rather then the center. Do you know why this happens?

Does that happen in the place file I sent? When the real mouse exits its bounds, it should be snapped to the middle due to modal being disabled for a single frame.

It still works for me but overall I think using modal for it is quite hacky. Are you planning on using the manual mouse moving in first person?

I’m planning on using it in third person, for a helmet camera system. This is what happens when I try and use it. Yes, it happens in the place you sent.

Ohh I think it has to do with the value 58 on this row:

fakeMouse.Position = UDim2.fromOffset(math.clamp(newPos.X, 0, sizeX), math.clamp(newPos.Y + 58, 0, sizeY))

It’s the vertical size of the Roblox top bar GUI. The bar seems to be different for me for some reason:
image

If you want the mouse to work with everyone you can replace the 58 with the following variable:

local topBarOffset = fakeMouseGui.AbsolutePosition.Y

Like this right? It seems to keep the mouse at the top which leads me to believe that I’m doing something wrong.

Oh nvm it should be subtracted since topBarOffset is negative:

fakeMouse.Position = UDim2.fromOffset(math.clamp(newPos.X, 0, sizeX), math.clamp(newPos.Y - topBarOffset, 0, sizeY))

It keeps it in the middle now, but it isn’t being nudged anymore. I noticed when testing it myself that replacing the 58 with other numbers makes the intensity of the nudging more or less, do you have any possible fixes. Thank you for bearing with me :sob:

Try integrating this in your code:

local nudgeX = 10 -- Adjust this value to control the nudge intensity on the X axis
local nudgeY = 10 -- Adjust this value to control the nudge intensity on the Y axis

fakeMouse.Position = UDim2.fromOffset(
    math.clamp(newPos.X + nudgeX, 0, sizeX),
    math.clamp(newPos.Y - topBarOffset + nudgeY, 0, sizeY)
)

You can use it to adjust the nudgeX and nudgeY values
Are you able to show the full script?

Yeah, let me get the full script.

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local plr = game.Players.LocalPlayer
local playerGui = plr.PlayerGui
local mouse = plr:GetMouse()
local cam = workspace.CurrentCamera

local fakeMouseGui = playerGui.FakeMouseGui
local centerLock = fakeMouseGui.CenterLock -- Used to recenter real mouse when it leaves its bounds
local mouseBounds = fakeMouseGui.MouseBounds -- Frame inside of which the real mouse can move
local fakeMouse = fakeMouseGui.FakeMouse

-- Moves fake mouse frame based on real mouse's movement
local function MoveFakeMouse(xOffset, yOffset)
	local newPos = fakeMouse.AbsolutePosition + Vector2.new(xOffset, yOffset)
	local sizeX, sizeY = cam.ViewportSize.X, cam.ViewportSize.Y
	local topBarOffset = fakeMouseGui.AbsolutePosition.Y
	fakeMouse.Position = UDim2.fromOffset(math.clamp(newPos.X, 0, sizeX), math.clamp(newPos.Y - topBarOffset, 0, sizeY))
end

--UserInputService.MouseIconEnabled = false

local prevPos = Vector2.new(mouse.X, mouse.Y)
RunService.RenderStepped:Connect(function(deltaTime)
	-- Force MouseBehaviour to be LockCenter so modal recentering hack works in 3rd person as well
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	
	local currentPos = Vector2.new(mouse.X, mouse.Y)
	local posOffset = (currentPos - prevPos)
	MoveFakeMouse(posOffset.X, posOffset.Y)
	
	local startCorner = mouseBounds.AbsolutePosition
	local endCorner = mouseBounds.AbsolutePosition + mouseBounds.AbsoluteSize
	if mouse.X > endCorner.X or mouse.X < startCorner.X or mouse.Y > endCorner.Y or mouse.Y < startCorner.Y then
		-- Real mouse left the bounds and will be moved to center of screen
		centerLock.Modal = false
		prevPos = mouseBounds.AbsolutePosition + mouseBounds.AbsoluteSize / 2
	else
		-- Real mouse is inside its bounds
		centerLock.Modal = true
		prevPos = currentPos
	end
end)
1 Like

Try this new script to see if it fixes it

local RunService = game:GetService(“RunService”)
local UserInputService = game:GetService(“UserInputService”)

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local cam = workspace.CurrentCamera

local screenCenter = Vector2.new(cam.ViewportSize.X / 2, cam.ViewportSize.Y / 2)
local pullStrength = 0.05 – Adjust this value to change the strength of the pull towards the center

RunService.RenderStepped:Connect(function()
local mousePos = Vector2.new(mouse.X, mouse.Y)
local offset = screenCenter - mousePos
local distance = offset.Magnitude
local pull = offset.Unit * math.min(distance * pullStrength, distance)

-- Calculate new mouse position
local newMousePos = mousePos + pull

-- Move the real mouse position
mouse.X = newMousePos.X
mouse.Y = newMousePos.Y

end)

Doesn’t work. Mouse position is read only. Thank you though.

1 Like

Not really sure what could be causing the mouse not to move. Can you send a video showing whats happening?
Btw here’s the place file but with the top bar fix, can you try it out and see if the mouse works there:
FakeMouse_Test2.rbxl (59.5 KB)