Trying to make a UI that acts like a mouse

I’m trying to make a custom ui that acts like a mouse using raycast and i am currently half way there but I am having trouble about it the cursor is suppose to print the Grass not the Baseplate and its position.

LocalScript Location:
image

local player = game:GetService("Players").LocalPlayer

local RunService = game:GetService("RunService")

local camera = workspace.CurrentCamera

function CursorRaycast()
	local cursor = player.PlayerGui:WaitForChild("Mouse").Cursor
	local cursorPos = {
		X = cursor.Position.X,
		Y = cursor.Position.Y
	}
	local cursorRay = camera:ViewportPointToRay(cursorPos.X.Scale, cursorPos.Y.Scale)

	local rayResult = workspace:Raycast(cursorRay.Origin, cursorRay.Direction * 100)
	
	return rayResult
end

RunService.RenderStepped:Connect(function()
	local result = CursorRaycast()
	
	if result and result.Instance then
		print(result.Instance.Name)
		print(result.Instance.Position)
		player.PlayerGui.Mouse.print.Text = result.Instance.Name
	end
end)

Gui:
image

Sorry for bad grammar…

I believe what you’re looking for is ScreenPointToRay.

Almost but also trying to make it work to mobile…

It should work just the same on mobile. Is there another issue elsewhere?

Oh yea, works on mobile but i want it to be like only on the center… In mobile you can do it anywhere you tap your screen… sorry for bad grammar

local camera = workspace.CurrentCamera
local viewportSize = camera.ViewportSize
local ray = camera:ScreenPointToRay(viewportSize.X/2, viewportSize.Y/2, 1)


I want it to only work on that cursor… no other locations

Then use the AbsolutePosition of the cursor?

The cursor thing is a image label…
im trying to make the image lable print where its pointing… The problem is that the image label is pointing on the grass but instead of it printing the “Grass” it prints out the baseplate

local cursorRay = camera:ViewportPointToRay(cursorPos.X.Scale, cursorPos.Y.Scale)

I think the only problem is the position thing…

It being an ImageLabel doesn’t matter as it still has an AbsolutePosition. ScreenPointToRay takes 3 arguments. The X pixel position on screen, the Y pixel position on screen, and the depth (how many studs to cast the ray to).

That is indeed the problem as Scale is not based on pixels, scale is a percentage of the total screenspace. You should be using AbsolutePosition or the Offset of the position.

I tried the offset but its still the same as the scale…

Can you upload a place file so I can edit it and see?

Heres the place that im using Workplace [R6] 2.0 - Roblox

You can find the gui on the StarterGui called “Mouse” and the local script on the StarterPlayerScripts called “CustomCursor”

I’m not entirely sure what the issue is yet, but I made a yellow ball appear where the raycast is hitting compared to the UI cursor:

Do you know how to make the raycast hit where the ui is pointing?

Oh, my bad. I was using AbsoluteSize instead of Position.

local player = game:GetService("Players").LocalPlayer

local RunService = game:GetService("RunService")

local camera = workspace.CurrentCamera

function CursorRaycast()
	local cursor = player.PlayerGui:WaitForChild("Mouse").Cursor
	local cursorPos = cursor.AbsolutePosition
	local cursorSize = cursor.AbsoluteSize
	local cursorRay = camera:ScreenPointToRay(cursorPos.X+(cursorSize.X/2), cursorPos.Y+(cursorSize.Y/2))
	return workspace:Raycast(cursorRay.Origin,cursorRay.Direction * 9e9)
end

RunService.RenderStepped:Connect(function()
	local result = CursorRaycast()
	
	if result and result.Instance then
		print(result.Instance.Name)
		print(result.Instance.Position)
		player.PlayerGui.Mouse.print.Text = result.Instance.Name
	end
end)
1 Like

Wait does the raycast now hit where the ui is pointing? Sorry i cant really test it rn cause im not at home just using my phone…

Yes, the dot now shows exactly where the Gui cursor is.

1 Like