Mouse position on screen in 3D space

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”?

Camera.Focus is positioned at the origin of the camera and looking at the mouse.

Attach also does so, but it only disabled one axis of turning.

well uh I used a script that i have used a lot, a ray casting script, but for some reason when I use camera.focus.position as the origin, and camera.focus.lookvector as the direction, it returns nothing

also i printed the camera.focus.position, i dont really think its the origin

That’s one of the disadvantages of setting the camera type to Scriptable. You should try Fixed.

Also, Focus is capitalized.

True. I think you should be using the camera’s CFrame instead. I strongly recommend not using the Mouse object for any of this.

how do I get the direction? like how do I make it point at the mouse. Do you mean the pospart, or just mouse in general?

You need to get the mouse’s position from UserInputService, first. Then,

Use this.

is this kinda what your trying to achieve?

1 Like

Not me, but OP probably is (don’t worry I know it’s an accident).

1 Like

yeah i just click the last reply in the post

well sorta, just with the camera’s rotation locked, and there will be parts in the way like the game getting over it. I tried the first link, and I ended up with some weird effect where the part was very close to my screen and it would go slightly closer as my mouse came to the edge of my screen.

I would use Raycasting here.

Here’s something I just quickly whipped up.

https://gyazo.com/5644486aa7c5f512d1000348889a13a3

local UIS = game:GetService("UserInputService")

local Part = Instance.new("Part")
Part.Color = Color3.fromRGB(255, 0, 0)
Part.Parent = workspace
Part.Anchored = true
Part.CanCollide = false

local function Raycast(X, Y)
	local Cam = workspace.CurrentCamera
	
	local Ray = Cam:ScreenPointToRay(X, Y)
	
	local Params = RaycastParams.new()
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	Params.FilterDescendantsInstances = {Part}
	
	local Raycast = workspace:Raycast(Ray.Origin, Ray.Direction * 500, Params)
	
	return Raycast.Position
end

UIS.InputBegan:Connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		local Pos = Raycast(input.Position.X, input.Position.Y)
		Part.Position = Pos
	end
end)

UIS.InputChanged:Connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		local Pos = Raycast(input.Position.X, input.Position.Y)
		Part.Position = Pos
	end
end)
5 Likes

you need to write a custom camera script and probably change the cameras FOV to make it more 2d

oh here is the code for the gif up above if you want to use it

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Mouse.TargetFilter =  workspace.MousePart

while wait() do
	workspace.MousePart.CFrame = CFrame.new((Mouse.Hit - Vector3.new(0,0,Mouse.Hit.Position.Z)).Position)
end