Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

Keeping a model to a grid?!

Asked by
legosweat 334 Moderation Voter
9 years ago

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:

1Item:SetPrimaryPartCFrame(CFrame.new(Mouse.Hit.p.X - Mouse.Hit.p.X%5, yAxis, Mouse.Hit.p.Z - Mouse.Hit.p.Z%5)) 

1 answer

Log in to vote
2
Answered by
drahsid5 250 Moderation Voter
9 years ago

It's pretty easy, if your question is "How do I snap it to a grid".

01local 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.
03end
04 
05local function snap(p,gW,gD) --The snap function, it should have three parameters, the position, the Grid width and Grid depth
06    return Vector3.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        )
11end

Next you would write a function that snaps it's position when you want it to.

I.E.

1button.MouseButton1Clicked:connect(function()
2    game.Workspace.SnapPart.CFrame=snap(game.Workspace.SnapPart.CFrame,5,5)
3end)

Sources: Math functions, Using return, Gui click, CFrame, Vector3

0
How will I perhaps move the model on a 5x5 grid? Like, make it move every 5 studs? legosweat 334 — 9y
0
Pass the mouse.hit.p through the snap function and set the brick's CFrame to what it returns, for your specific problem. drahsid5 250 — 9y
Ad

Answer this question