How can I 'round' a Vector3 to the nearest stud?
I'm trying to create an equivalent to the default ROBLOX 'Drag' HopperBin, but I want it to only work within a 20 stud radius. I've got that bit working, but actually moving the part I'm dragging is not how I'd like it. I want it 'rounded' to the nearest stud. The problem with that is that some studs are .5 and not just a solid 1. Here's my current code:
local player = script.Parent.Parent.Parent.Parent
local mouse = player:GetMouse()
local draggable = false
local trg = nil
01 | mouse.Move:connect( function () |
03 | if mouse.Target ~ = trg then |
04 | if (mouse.Hit.p - trg.Position).magnitude < = 50 then |
05 | trg.Position = Vector 3. new(math.ceil(mouse.Hit.p.X),math.ceil(mouse.Hit.p.Y),math.ceil(mouse.Hit.p.Z)) |
11 | mouse.Button 1 Down:connect( function () |
12 | if mouse.Target ~ = nil then |
13 | if mouse.Target:findFirstChild( "ForageItem" ) ~ = nil then |
14 | if mouse.Target:findFirstChild( "ForageItem" ).RemoveOnForage.Value = = true then |
15 | if mouse.Target:findFirstChild( "NoDrag" ) = = nil then |
16 | if player.Character ~ = nil then |
17 | if (mouse.Target.Position - player.Character:GetModelCFrame().p).magnitude < = 50 then |
24 | if draggable = = true and trg ~ = nil then |
25 | trg.Rotation = Vector 3. new( 0 , 0 , 0 ) |
26 | local sel = Instance.new( "SelectionBox" ,player.PlayerGui) |
29 | sel.Color = BrickColor.new( "Bright blue" ) |
32 | if player.PlayerGui:findFirstChild( "DragBox" ) ~ = nil then |
33 | player.PlayerGui:findFirstChild( "DragBox" ):Destroy() |
39 | mouse.KeyDown:connect( function (key) |
41 | trg.CFrame = trg.CFrame * CFrame.Angles( 0 ,math.pi/ 2 , 0 ) |
42 | elseif key = = "t" then |
43 | trg.CFrame = trg.CFrame * CFrame.Angles( 0 , 0 ,math.pi/ 2 ) |
44 | if trg.Rotation.Z = = 90 then |
45 | trg.Position = trg.Position + Vector 3. new( 0 , 2 , 0 ) |
50 | mouse.Button 1 Up:connect( function () |
51 | if player.PlayerGui:findFirstChild( "DragBox" ) ~ = nil then |
52 | player.PlayerGui:findFirstChild( "DragBox" ):Destroy() |
62 | coroutine.resume(coroutine.create( function () |
65 | if player.PlayerGui.TopBar.Selected.Value = = "Drag" then |
66 | script.Parent.Visible = true |
67 | script.Parent.Position = UDim 2. new( 0 ,mouse.X + 10 , 0 ,mouse.Y + 40 ) |
69 | if mouse.Target ~ = nil then |
70 | if mouse.Target:findFirstChild( "ForageItem" ) ~ = nil then |
71 | if mouse.Target:findFirstChild( "ForageItem" ).RemoveOnForage.Value = = true then |
72 | if mouse.Target:findFirstChild( "NoDrag" ) = = nil then |
73 | if player.Character ~ = nil then |
74 | if (mouse.Target.Position - player.Character:GetModelCFrame().p).magnitude < = 50 then |
82 | if passed = = false then |
83 | script.Parent.Text = "Not Draggable" |
84 | script.Parent.TextColor 3 = Color 3. new( 255 / 255 , 0 , 0 ) |
86 | script.Parent.Text = "Draggable" |
87 | script.Parent.TextColor 3 = Color 3. new( 0 , 255 / 255 , 0 ) |
91 | script.Parent.Visible = false |