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.
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)
local camera = workspace.CurrentCamera
local viewportSize = camera.ViewportSize
local ray = camera:ScreenPointToRay(viewportSize.X/2, viewportSize.Y/2, 1)
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
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.
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)