Mouse position on screen in 3D space

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