Any opinions on how I'd keep a 20x20 model on a 5x5 grid?
I've already tried some other code, but it didn't work, well it did, but it was inaccurate.
My code:
1 | Item:SetPrimaryPartCFrame(CFrame.new(Mouse.Hit.p.X - Mouse.Hit.p.X% 5 , yAxis, Mouse.Hit.p.Z - Mouse.Hit.p.Z% 5 )) |
It's pretty easy, if your question is "How do I snap it to a grid".
01 | local function round(i) --You make a function to round to the nearest number, it would have a single parameter. |
02 | return math.floor(i+. 5 ) --math.floor + .5 will round to the nearest. |
03 | end |
04 |
05 | local function snap(p,gW,gD) --The snap function, it should have three parameters, the position, the Grid width and Grid depth |
06 | return Vector 3. new( |
07 | round(p.x / gW)*gW, --Simple equation, shouldn't need to be explained as it is really that simple. |
08 | 1 , --You could also use the rounded number of p.y /a grid height * a grid height for this. |
09 | round(p.z / gD)*gD --repeat the equation. |
10 | ) |
11 | end |
Next you would write a function that snaps it's position when you want it to.
I.E.
1 | button.MouseButton 1 Clicked:connect( function () |
2 | game.Workspace.SnapPart.CFrame = snap(game.Workspace.SnapPart.CFrame, 5 , 5 ) |
3 | end ) |
Sources: Math functions, Using return, Gui click, CFrame, Vector3