UI Cursor relative to the player's mouse position

I’ve been attempting to set a cursor image label inside a surfaceGUI adornee’d to a part relative to the player’s mouse position. I’ve looked for solutions, but they all have caused the same/new problem.

The problem

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

local players = game:GetService("Players")
local localPlayer = players.LocalPlayer

local mouse = localPlayer:GetMouse()
local currentCamera = workspace.CurrentCamera

local computerGui = script.Parent

local display = computerGui.Display
local wallpaper = display.Wallpaper
local cursor = wallpaper.Cursor

local function UpdateCursorPos()
	local mouseTarget = mouse.Target
	if mouseTarget == computerGui.Adornee then
		local mousePos = userInputService:GetMouseLocation()
		cursor.Position = UDim2.fromOffset(mouse.X - cursor.Position.X, mouse.Y - cursor.AbsolutePosition.Y)
		mouse.Icon = "http://www.roblox.com/asset/?id=7649231250"
		currentCamera.CameraType = Enum.CameraType.Scriptable
		currentCamera.CFrame = workspace.ComputerCameraPart.CFrame
		print(cursor.Position)
	else
		userInputService.MouseIconEnabled = true
		currentCamera.CameraType = Enum.CameraType.Custom
	end
end

runService.RenderStepped:Connect(UpdateCursorPos)
1 Like

Try imageLabel.Position = UDim2.new(0,mouse.X,0,mouse.Y)

Unfortunately doesn’t work, as far as I’ve understood (I’m horrible at math) I should be on the right track with calculating the relative positions of X and Y with:

cursor.Position = UDim2.fromOffset(mouse.X - cursor.Position.X, mouse.Y - cursor.AbsolutePosition.Y)
2 Likes

Well try this:

UDim2.new((mouse.X)/workspace.CurrentCamera.ViewportSize.X,0,(mouse.Y)/workspace.CurrentCamera.ViewportSize.Y,0)

Same problem still stands:
Problem

1 Like

Well try somehow to use scale instead of offset, that would fix your problem.

The fix was actually incredibly simple, I just used the MouseMoved event. Here’s the fully working code (without any of the debugs or unnecessary variables I forgot in it:

local userInputService = game:GetService("UserInputService")

local players = game:GetService("Players")
local localPlayer = players.LocalPlayer

local mouse = localPlayer:GetMouse()
local currentCamera = workspace.CurrentCamera

local computerGui = script.Parent

local display = computerGui.Display
local wallpaper = display.Wallpaper
local cursor = wallpaper.Cursor

local function UpdateCursorPos(X, Y)
	local mouseTarget = mouse.Target
	if mouseTarget == computerGui.Adornee then
		cursor.Position = UDim2.fromOffset(X,Y)
		currentCamera.CameraType = Enum.CameraType.Scriptable
		currentCamera.CFrame = workspace.ComputerCameraPart.CFrame
		userInputService.MouseIconEnabled = false
	else
		currentCamera.CameraType = Enum.CameraType.Custom
		userInputService.MouseIconEnabled = true
	end
end

display.MouseMoved:Connect(UpdateCursorPos)
3 Likes

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