How do I find an equivalent of mouse.Hit for a 2D game?

In my 2D project on Roblox, I’ve been using Mouse.Hit for things like aiming weapons and, specifically right now, making the character look at the mouse’s position.

However, since the mouse will typically be pointing at the skybox, Mouse.Hit.p gives values like -2139.658935546875, 5608.1044921875, -7989.4189453125, when I would want something like (100, 30, 0). Obviously, I can multiply this Vector3 by (1, 1, 0) to get rid of the Z-value, but it’s still the wrong X and Y values, which is a problem.

I’ve looked on the forums and tried Camera:WorldToScreenPoint with UserInputService:GetMouseLocation among other things, but nothing seems to work.
So, how do I get an accurate position of the mouse in this case?

An image of my game so you can get the idea of what I’m doing:

(while the image shows that the character is looking at the mouse in this picture, at most angles it doesn’t work, and I’ve confirmed that the problem is with mouse.Hit)

3 Likes

Make the background an invisible part, that way it would be more accurate to the (100,30,0) and the mouse isn’t going in the void.

Oh shoot, I forgot to mention that I tried that. When I did that, the mouse position got messed up like this: (The blue part is where mouse.Hit is)



(background is slightly opaque so you can see it)

Is there something wrong I’m doing with this?

	local MouseHit = mouse.hit.p * Vector3.new(1,1,0)
	part2.Position = MouseHit --part to show where mouse.hit is

Not multiplying it by (1, 1, 0) didn’t help either, unfortunately.

Nevermind! As it turned out, because in a different script the camera was being modified, the camera’s CFrame was being set after this script got the mouse.Hit, so it was getting the wrong mouse.Hit
Problem solved.

1 Like