How would I go about getting the exact 3D CFrame of the mouse cursor?

I have come up with this function, but sadly it seems to always be off by quite a bit, which makes it unusable for my cause.

function GetMouseCFrame()
	local MousePos =  UIS:GetMouseLocation()
	local Magnitude1Ray = cam:ScreenPointToRay(MousePos.X,MousePos.Y)--original ray can only be 1 stud long
	local NewRay = Ray.new(Magnitude1Ray.Origin,Magnitude1Ray.Direction* 1000)--replicated ray to get greater length
	local Target, Position = workspace:FindPartOnRay(NewRay, plr.Character)
	return Target
end
print(GetMouseCFrame().Name)

Thanks in advance![2021-06-16 22-29-33_Trim|video](upload://w6WIlBO3jJUPoU6ME1cqPq4Gx3b.mp4)

1 Like

The mouse is based on the client and has a position on the screen in 2d. This means that you can’t have a CFrame value with the mouse. The mouse’s 3D position can get raycasted using a bunch of math. But that only gives you where the mouse is pointing at. Please give us more information on what you are trying to achieve.

use the localPlayer Mouse and do Hit (this would be the exact 3D CFrame).

local mouse = game.Players.LocalPlayer:GetMouse()

function GetMouseCFrame()
	return mouse.Hit
end

print(GetMouseCFrame())

But it seems like you are trying to get the mouse target, so it would be this


local mouse = game.Players.LocalPlayer:GetMouse()

function GetMouseTarget()
	return mouse.Target
end

print(GetMouseTarget().Name)
1 Like

I want the 3D CFrame of the position that the mouse is pointing at.

Thank you, but I am purposefully trying to solve this not using the legacy mouse but the UserInputService since my entire game uses it