Problems with Mouse Positioning (Mouse.UnitRay)

Hello Fellow Developers,
(i understand that this is a long issue, so you can skip to the TL;DR)

I have run into the issue of Using Mouse.UnitRay and the mouse positioning gets extremely inaccurate,
– Background Info (read this for the rest to make sense)
I am currently creating a game with a placement system (like a Furniture placement system, but using it for a survival game, so you can place wood blocks/planks etc…)
I have coded a placement handler system which creates a “ghost” part which the user can rotate and move around and then click to place the item in the real world (on the server),
– My problem
the problem is when I use the Mouse.UnitRay as my primary way to have custom placement, it works fine for distances, but I have added a distance limit of 40 studs (obviously, you don’t want people placing stuff around the map) I have also added an advanced rotation system, the part goes red which indicates it is farther than 40 studs or green if closer and you can place it, so when I use this rotation system, the Unit ray thinks it is farther than 40 studs to my character which it is not, this only seems to happen when I try to rotate this along some axis, I am very confused here, I’m not sure if Roblox has depreciated this function or there is some better alternative

Ok so the TL;DR is Mouse.UnitRay thinks a part is way farther than it should be when a part has added rotations onto it, I’m not sure if my code is the problem or that there is a better way to solve this issue. also, this issue only sometimes occurs which is even more confusing

ok so here is a sample of the code, that was causing the issue, there are a lot of variables so yeah, this script is heavily embedded to other parts of my game

function handleRenderStepped()
	local mouseRay = mouse.UnitRay
	local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 50)-- only *50 is 
 --needed
	local ignoreList = {clientStructure, char} -- ignores the "ghost" part itself and the players character
	local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)
	


	 --client structure just means the "ghost" part before placement
	
	DistText.Text = ""..math.floor((HumanoidRootPart.Position clientStructure.Position).Magnitude).." Studs Away From You"
	DistText.Font = Enum.Font.GothamBlack

if hit and (HumanoidRootPart.Position - clientStructure.Position).Magnitude < maxPlacingDistance then
		goodTOPlace = true
		clientStructure.BrickColor = BrickColor.new("Forest green") -- can place the item 
	else
		goodTOPlace = false
		clientStructure.BrickColor = BrickColor.new("Crimson") -- too far away to place the item
	end

	local newAnglesCFrame = CFrame.Angles(math.rad(xOrientation),math.rad(yOrientation),math.rad(zOrientation)) orientation of the item
	local newCFrame = CFrame.new(position.X, position.Y + yBuildingOffset, position.z)
	clientStructure.CFrame = newCFrame * newAnglesCFrame
end

this is how it’s supposed to look and act
(i had to compress the video so there is a watermark but that shouldn’t affect anything)

This is my problem and how it sometimes acts

ok so I don’t expect anyone to rewrite my whole script but any suggestions on how to be able to solve this would be very good, I will provide scripts requested and if anyone could tell me how this could be solved and if there is another way to change my script then please reply, I am respectful for any reply that may help me to fix this annoying issue,

if you’re still with me I congratulate you, thanks for reading!

I was wondering if the problem is that the Magnitude changes when you rotate it. Though it doesn’t make sense why it thinks its 50 studs away. Maybe try replacing this line:

Change to this:

if hit and (HumanoidRootPart.Position - (position + Vector3.new(0,yBuildingOffset,0))).Magnitude < maxPlacingDistance then
1 Like

it seems that this does make the problem somewhat better, but if you keep rotating it will
increase its distance by a stud eventually leading to it being 50 studs away which isn’t ideal,
thankyou for the support @XdJackyboiiXd21 but I think I have to change a bit more than that for the issue to be fully gone

ok im see if the problem is the magnitude so im going to add a print statement to print the magnitude

ok so I changed a few variables and added Vector3 instead of Magnitude (because magnitude can have very very bad Floating Point Error’s, which was the cause of my
the problem, my code wasn’t bad, its just whenever you rotate long enough the Magnitude’s margin of error increases for some reason, but I don’t get any problem with Vector3, Thanks for helping me solve this problem, I will now on use Vector3 as much as possible as it makes more sense to use, as you can use CFrame’s with it much better, Thanks for the help :smiley:

1 Like