Mouse position isn't accurate at the top and bottom of the screen

I am making a click effect and to do this I need to find the location of the mouse on the screen. I intend for this effect to happen in the middle of the mouse.
This works fine when I click in the middle of screen as you can see from this image:
image
But when clicking at the top or bottom of my screen the mouse position seems to be vertically inaccurate as the position is either too high or low:
image
This is the script I am using to do this:

local userInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local playerGui = player:WaitForChild("PlayerGui")
local ScreenGui = playerGui:WaitForChild("ScreenGui")

local replicatedStorage = game:GetService("ReplicatedStorage")
local mouseParticleTemplate = replicatedStorage:WaitForChild("mouseParticle")

local tweenService = game:GetService("TweenService")

local goal = {}
goal.ImageTransparency = 1

local tweenInfo = TweenInfo.new(
	0.1,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false
)

local function onInputBegan(input, _gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local mouseParticle = mouseParticleTemplate:Clone()
		mouseParticle.Parent = ScreenGui
		mouseParticle.Position = UDim2.new(mouse.x/mouse.ViewSizeX,0,mouse.y/mouse.ViewSizeY,0)
		mouseParticle.Visible = true
		mouseParticle:TweenSize(UDim2.new(0.055, 0,0.101, 0),"Out", "Linear", 0.1)
		local tween = tweenService:Create(mouseParticle, tweenInfo, goal)
		tween:Play()
	end
end

userInputService.InputBegan:Connect(onInputBegan)

Any thoughts on how I could get the mouse location more accurate?

1 Like

That has fixed the issues with the position being different at the top/bottom of the screen however now it doesn’t come from the middle of the mouse
image

1 Like

Just saw that now, I’m gonna check.

1 Like

I think it has to do with the GUI Inset.

https://create.roblox.com/docs/reference/engine/classes/GuiService#GetGuiInset

You should account for it when creating the circle.

2 Likes

With the IgnoreGuiInset property set to false its now fine at the top of the screen but at the bottom it still isn’t in the middle.

1 Like

Along with the GUI Inset as mentioned previously

Try using mouse.ViewportSize instead, the values seem different:

2 Likes

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