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

How could I accomplish a placement system with grid size over 1x1?

Asked by 7 years ago
Edited 7 years ago

Here's kind of what I'm looking to do:

Twitter Gif Link Thing

I know I can use:

math.floor() and math.ceil() to round up and down. I also know I can create a rounding function:

local function round(number)
    return math.floor(number + 0.5) -- Rounds to the nearest whole number
end

I would like to know how I could accomplish this with a grid size over 1x1. Thanks!

0
There are two options. You could either have it detect the mouse position which would check the nearest place for a tile, or you could have tiles and use mouse.Target. MrLonely1221 701 — 7y
0
Divide mouse pos by grid, add 0.5, round, and mutiply by grid (after rounding). zblox164 531 — 5y
0
Made an answer even though this is a year old. LOL zblox164 531 — 5y

1 answer

Log in to vote
1
Answered by
zblox164 531 Moderation Voter
5 years ago

Simple solution!

*Divide mouse position by grid, add 0.5, round either up or down, and mutiply by grid (after rounding). You should be using a LocalScriptfor this.

Example:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Part= workspace.Part

local PosX, PosY, PosZ -- We will set these later

local function Round(GridSize)
    PosX = math.floor(mouse.Hit.X / GridSize + 0.5) * GridSize -- Snaps
    PosY = 0.5
    PosZ = math.floor(mouse.Hit.Z / GridSize + 0.5) * GridSize -- Snaps
end

local function Movement()
    mouse.TargetFilter = Part -- Ignores the model

    Round(2) -- Pass your grid size as an argument

    Part.Position = Vector3.new(PosX, PosY, PosZ) -- Positions
end

mouse.Move:Connect(Movement)

I KNOW I know that this is a year old but I just had to answer this. Hope this helps... if you see this.

0
I accepted your answer a year later :) I hope you know I eventually did create an extremely smooth grid system soon after asking this question AstrealDev 728 — 3y
Ad

Answer this question