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

A basic walkthrough of a grid placement system?

Asked by
waifuSZN 123
7 years ago

I'm looking into creating a tycoon from scratch, and the first thing I want to tackle is the grid placement system.

I'm unsure what this entails, so some wiki links and/or basic examples would help me out alot to understand this.

Thanks!

0
a guy on youtube named DutchDeveloper have made a miners haven replica Filipalla 504 — 7y

1 answer

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

I know this is a year old but I thought I could help anyway.

First you want to create a function to do the movement. To move a part to the mouse position you use Mouse.Hit. I am creating three variables one for each axis. Also I am creating two functions that will be used later on in the script. Make a simple model with a primary part so that we can move the model around in our space.

01-- Define variables
02local Player = game.Players.LocalPlayer
03local Mouse = Player:GetMouse() -- Gets the mouse
04 
05local PosX -- X
06local PosY -- Y
07local PosZ -- Z
08 
09local Model = game.ReplicatedStorage.Model:Clone() -- Model you have ready
10 
11-- Main script
12 
13local function Snap()
14    --Sets variables
15 
View all 34 lines...

To snap to a grid we round either up or down to the nearest whole. math.floor()and math.ceil() will work for snapping. math.floor() rounds down and math.ceil() rounds up

01-- Define variables
02local Player = game.Players.LocalPlayer
03local Mouse = Player:GetMouse() -- Gets the mouse
04 
05local PosX -- X
06local PosY -- Y
07local PosZ -- Z
08 
09local Model = game.ReplicatedStorage.Model:Clone() -- Model you have ready
10 
11-- Main script
12 
13local function Snap()
14    --Sets variables
15 
View all 34 lines...

You will notice that the model is snapping but only on a 1x1x1 grid. To add grid size it gets a little more complex (not really though). First you divide the mouse pos by the grid then add 0.5 then round and finally multiply by grid size. Example:

01-- Define variables
02local Player = game.Players.LocalPlayer
03local Mouse = Player:GetMouse() -- Gets the mouse
04 
05local PosX -- X
06local PosY -- Y
07local PosZ -- Z
08 
09local GridSize = 2 -- Example
10 
11local Model = game.ReplicatedStorage.Model:Clone() -- Model you have ready
12 
13-- Main script
14 
15local function Snap()
View all 36 lines...

This is the simplest way to snap to a grid. Hope this helps (if you see this.)

0
Highly appreciated! Something new for me to understand. waifuSZN 123 — 6y
0
No problem! zblox164 531 — 6y
Ad

Answer this question