I am attempting to recreate the Roblox Dragger class as it is incompatible with FilteringEnabled. I am far from decent when it comes to 3D mathematics and CFraming. I have mustered up this piece of code:
local MouseRay = Ray.new( Mouse.UnitRay.Origin, Mouse.UnitRay.Direction * 999 ) local IgnoreList = { Player.Character, DragPart } local Hit, Position, Normal = workspace:FindPartOnRayWithIgnoreList( MouseRay, IgnoreList ) --local Position = Vector3.new( math.floor( Position.x ), math.floor( Position.y ), math.floor( Position.z ) ) local Shifted = Hit.CFrame + ( ( DragPart.Size/2 ) * Normal ) local MousePosition = Hit.CFrame:toObjectSpace( CFrame.new( Position ) ):inverse() Shifted = Shifted - MousePosition.p DragPart.CFrame = Shifted
It works in a very mediocre fashion for only a single part and completely falters once any rotation is applied to the moused over part.
The main question is - How do I align the part I am moving with the 'grid' of the part I am moused over? This is all to achieve the goal of moving a part along the grid in 1 stud increments much like the 'Dragger' class.
Thanks in advance for any provided help.
I'm no expert when it comes to 3D mathematics (I'm on a steady learning pace) but if I'm not mistaken you may have to manually snap the part to a predetermined position along with use of math.ceil(x)
because it will round a number up to it's nearest whole, and math.floor(x)
will round it down to it's nearest whole.
(Also if you want to automatically round up or down depending on the decimal, then you could write a rounding function.)
function numberRound(number) local High = math.ceil(number) - number local Low = number - math.floor(number) if High > Low then return High elseif High < Low then return Low end end
Now like I said, I'm not entirely sure if this is correct but I hope this at least reveals any possibilities to fixing your problem (if it doesn't already)