Detecting when a UI "touches" a 3D Part

I decided to use a 8x8 white Frame UI on the middle of my screen as my mouse cursor, also hiding the actual cursor using UserInputService.MouseIconEnabled.

I’ve also wanted to add an interaction thing, where if your white frame UI “touches” a part, you can interact with it. Just like when a mouse hovers over a 3D part, you can use Mouse.Target, instead, I want to detect if a UI hovers over one.

I’ve tried googling and rephrasing my issue, and I found nothing that could help me.

If you have any suggestions or solutions, please go ahead and comment them below. Thank you.

What my white box UI looks like:

12 Likes

You should use raycasting for this. You want to shoot a ray from the camera position towards the camera direction. You can get this information in the camera.CFrame.

7 Likes

That could work, but there’s an issue.

My game also has view bobbing on, for a realistic camera, and for some reason in first person, my mouse cursor doesn’t properly intersect because of the bobbing.

So I tried using a UI to try intersecting, but I couldn’t find any way to do this.

Edit: I will try raycasting and see if the view bobbing breaks it or not.

4 Likes

The reason I’m doing this is because the mouse doesn’t properly intersect.

If you’re trying to intersect from the camera, then that camera does intersect from the current view bobbing position. I think this can work.

3 Likes

Well if you bob the camera then it would affect the camera’s CFrame. So if for example you shake the screen (by shaking the camera CFrame) then the intersected objects will also change during the shake.

Of course that is true with the UI as well. Actually the UI itself has no way to tell the objects behind it. Instead you could transform all objects from world to screen position and that would make it possible to intersect with the UI. But that’s bad, because you need to do this for all the objects in the scene. It doesn’t scale.

Back to the bobbing, it depends on how you want your game to act. Do you want the bobbing to affect the highlighting of items? If not, then make sure to save the actual camera CFrame (pre-bob) and use that to ray cast. Both are possible.

3 Likes

I’ll try out the raycast. Wait for me.

4 Likes

One more tip for raycasts. A lot of times you are not sure where your raycast hits while debugging it. I would suggest to make some marker parts that you create and destroy onRenderStepped so you can see where your raycast ends.

2 Likes

Would this work?

mouse.Move:Connect(function()
	local raycast = workspace:Raycast(camera.CFrame,camera.LookVector)
	local object = raycast.Instance
end)

Edit: This is in a local script, by the way.

2 Likes

Probably, and you can use raycast.Position to draw your marker.

2 Likes

Thank you for helping. I’ll mark your comment as a solution.

3 Likes

Forgot to add .CFrame there.
Another edit. Also forgot to add .Position to the CFrame.

mouse.Move:Connect(function()
	local raycast = workspace:Raycast(camera.CFrame.Position,camera.CFrame.LookVector)
	local object = raycast.Instance
end)
3 Likes

@Redridge

My raycast is only intersecting my head, which makes sense because it begins in the camera.
How do I ignore it?

2 Likes
local distance = 50

local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.FilterType.Exclude
rayParams.FilterDescendantInstances = {character}

mouse.Move:Connect(function()
	local raycast = workspace:Raycast(camera.CFrame.Position,camera.CFrame.LookVector * distance, rayParams)
	local object = raycast.Instance
end)

Also multiple the lookvector with a number (distance) since LookVector by default has a magnitude of 1 and will only raycast 1 stud from the camera

3 Likes

Thanks for helping me out, both @Mystxry12 and @Redridge

3 Likes

Give the solution to @Redridge cuz he’s the one who helped you the most

2 Likes

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