Mouse position on screen in 3D space

How can I convert the mouse’s position on the screen to the world? I’m trying to make a “better” ball and axe game, but I don’t know how they got the position thing. I used mouse.hit x and y, and just put the z to 0. But as you can see, the mouse.hit is faulty.


also anyone know how to disable right click camera thing?

CameraToggle can be disabled from the menu, although I personally like it in some cases.

You need to put the Z to 0 in world space, not object space.

Sorry the right click thing was hard to explain. I don’t mean cameraToggle, I meant the game thinking you can hold right click to rotate camera, but you can’t, and it just bugs out the part.

The z position for the part is 0 in world space, I don’t even know how to do it in object space.
Screenshot (552)

You need to multiply the part’s position by the camera vector. I’m sure you add all 3 of them and then just subtract the Z axis one.

multiply it by the cameras look vector or what vector?

This will give you the Z axis only. You want to multiply everything but the Z axis.

You also need to do math.abs() to make the value positive.

I have no idea what im doing so I tried my best to multiply it by the right and up vector but I just ended up with it staying at 0,0,0

	pospart.Position = 	pospart.Position * Vector3.new(camera.CFrame.RightVector,camera.CFrame.UpVector,0)

I think it may have to do with the camera. When I lock the camera’s rotation, it doesn’t work.

yeah its probably the camera. It bugs out even more when I zoom in and out

The part’s parent must be the character so TargetFilter works.

okay so, i figured a few things out. I set the camera’s min and max zoom to 20, which is how far away the camera is from the ball so it fixed the offset. Then I set the camera’s type to scriptable so it disabled the right click turning. The only thing left is figuring out how to set the mouse’s target filter to the map and the character.

Just do Mouse.TargetFilter = workspace. That will make it filter everything. Though at that point I would rather use the CFrame of Camera.Focus.

Well, I have the giant part that the mouse’s hit relies on. Also what is the focus thing? the developer thing it was for like lighting or area?

You can only set the target filter to one part at a time, unless you were to raycast.

I went back and tried doing the hit.position but with the z set to 0 and the target filter workspace, but the hit’s position if very far away and and it says the x and y is around 5000. What do I do now?

That’s the nature of the Mouse object.

why are you setting z to 0?

and have you looked at
https://developer.roblox.com/en-us/api-reference/function/Camera/ScreenPointToRay
or
https://developer.roblox.com/en-us/api-reference/function/Camera/ViewportPointToRay

or a raycast itself

1 Like

im making a 3d 2d game so im setting the z to 0 so the axe doesn’t try to go in front of the map

They call that a 2.5D game. It’s a real thing.

Oh, now I understand I would recommend making Camera.CameraType set to Attach.

Also, the world/object space shouldn’t matter. I do recommend raycasting from Camera.Focus instead.

i put it to scriptable so it disables right click turning, and I have it so that it follows the character via scripting.

and uh also but uh what is camera focus is it where the camera is pointing? or lighting? or “area”?