How do I get the absolute mouse center position?

Title says it all possibly a bit out of context.

When in first person, how do I get the mouses position (the pointer) on all devices.

Heres the problem I had, the grey frame you see is my previous test, which is constantly wrong, every time I re-enter It decides it wants to not be the middle anymore. Heres the code I used to get the frame:

local mouse = game.Players.LocalPlayer:GetMouse() local f =  Instance.new("Frame", game.Players.LocalPlayer.PlayerGui.Freecam) f.AnchorPoint = Vector2.new(.5,.5) f.Position = UDim2.new(0, mouse.X, 0, mouse.Y) f.Size = UDim2.new(.01, 0, 0.01, 0) 

I did that in the command bar, this is my result, obviously varying in the new test. (The mouse is no longer in the center of the frame ;/)

1 Like

You forgot about the Topbar. Its Y size is 36.

2 Likes

I set the Y offset to 36 and I got this now.

Its Y size is 36. However, to change Frame position into middle, you have to divide to 2; 36 / 2 = 18.

1 Like

36 pixels for the Topbar is subject to change. Make sure to use GuiService:GetGuiInset when formatting things.

3 Likes

Works in studio, but in-game I get this, why cant the mouse just be truly in the middle ;/

I’m having no issues running this code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local frame = script.Parent

local function Update()
	local position = UDim2.new(0,mouse.X,0,mouse.Y) -- frame.AnchorPoint is .5,.5
	frame.Position = position
end

game:GetService("RunService").Heartbeat:Connect(Update)

which yields this result:

You only need to account for the GuiInset if IgnoreGuiInset is set to true on the ScreenGui

1 Like

The thing is, I need the mouse locked in the center of the screen for a “Third Person Shooter” kinda deal. I need to make the mouse transparent, and make custom reticles that should be tweened in/out. I need the middle of the screen so I can raycast properly to then move on. :))

Check out some of the Camera properties and functions for Raycasting from a certain screen point:

https://developer.roblox.com/en-us/api-reference/function/Camera/ScreenPointToRay

The center of the screen is:

local camera = workspace.CurrentCamera
local viewport_size = camera.ViewportSize
local middle_screen = viewport_size / 2
5 Likes