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
8 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:

Item: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
8 years ago

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

local function round(i) --You make a function to round to the nearest number, it would have a single parameter.
    return math.floor(i+.5) --math.floor + .5 will round to the nearest.
end

local function snap(p,gW,gD) --The snap function, it should have three parameters, the position, the Grid width and Grid depth
    return Vector3.new(
        round(p.x / gW)*gW, --Simple equation, shouldn't need to be explained as it is really that simple.
        1, --You could also use the rounded number of p.y /a grid height * a grid height for this.
        round(p.z / gD)*gD --repeat the equation.
        )
end

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

I.E.

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

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 — 8y
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 — 8y
Ad

Answer this question