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

How can i make a 3D grid snapping and placement script?

Asked by 6 years ago

its like miners haven but you could make not just x and y grid snapping but z grids too like for building a house or a wall so you can build taller instead of just on a flat plane i tried doing something like it but nothing works :( please help

1 answer

Log in to vote
2
Answered by
zblox164 531 Moderation Voter
6 years ago
Edited 6 years ago

You will need to get set up first. Create a Model with a PrimaryPart. You can assign a PrimaryPart by clicking PrimaryPart in the properties menu. Then click on the object you want to be the PrimaryPart either in the explorer or the game view. You will want to put a LocalScript in a client side service such as StarterGui orReplicatedFirst (I have mine in StarterGui and it works fine). Here is a basic grid system example:

01local Model = workspace.Model -- Or where ever you have it
02local GridSize = 2 --Or whatever size you want in studs
03 
04local Player = game.Players.LocalPlayer
05local Mouse = Player:GetMouse()
06 
07Mouse.Move:Connect(function() -- fired when the mouse moves
08    Mouse.TargetFilter = Model -- Ignores the model
09    -- math.floor rounds down no matter how large the number is
10    -- This means even if you had 1.9 it would round down to 1.
11 
12    -- Snap to grid
13    local PosX = math.floor(Mouse.Hit.X / GridSize + 0.5) * GridSize
14    local PosY = Mouse.Hit.Y
15    local PosZ = math.floor(Mouse.Hit.Z / GridSize + 0.5) * GridSize
View all 22 lines...

Mouse.Hit.axis is your mouse position on that axis. MoveTo moves the model to a Vector3 value. As for the rounding it centers it with in the grid of 2 studs which you can change by changing the GridSize variable.

If you just want the grid to be 1x1x1 then an even simpler solution is required:

1PosX = math.floor(Mouse.Hit.X) -- Just did it for one axis
2-- This will make the part move on a 1 stud grid.

As for placing goes there is an event that you can use. Mouse.Button1Down should work. Example:

1Mouse.Button1Down:Connect(function()
2    local PlacedModel = Model:Clone() -- Creates a clone of the current model
3    Model:Destroy() -- Destroys the moving model
4 
5    -- Stop placement code below
6 
7    -- Thats for you to do
8end)

Hopefully this helps!

0
does the primary part have to be a certian size or just any size mattchew1010 396 — 6y
0
I would make it a little bigger than the model. zblox164 531 — 6y
Ad

Answer this question