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!