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!
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.
-- Define variables local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() -- Gets the mouse local PosX -- X local PosY -- Y local PosZ -- Z local Model = game.ReplicatedStorage.Model:Clone() -- Model you have ready -- Main script local function Snap() --Sets variables PosX = Mouse.Hit.X PosY = Mouse.Hit.Y PosZ = Mouse.Hit.Z end local function Movement() Mouse.TargetFilter = Model --Ignores the model Snap() -- Gets the variables ready (We will snap later) Model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ)) -- Moves the model end -- Starts placement local function StartPlacement() Model.Parent = workspace end game.Players.PlayerAdded:Connect(StartPlacement) -- Simple so not explaining this
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
-- Define variables local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() -- Gets the mouse local PosX -- X local PosY -- Y local PosZ -- Z local Model = game.ReplicatedStorage.Model:Clone() -- Model you have ready -- Main script local function Snap() --Sets variables PosX = math.floor(Mouse.Hit.X) -- SNAP PosY = Mouse.Hit.Y PosZ = math.floor(Mouse.Hit.Z) -- SNAP end local function Movement() Mouse.TargetFilter = Model --Ignores the model Snap() -- Gets the variables ready (We will snap later) Model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ)) -- Moves the model end -- Starts placement local function StartPlacement() Model.Parent = workspace end game.Players.PlayerAdded:Connect(StartPlacement) -- Simple so not explaining this
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:
-- Define variables local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() -- Gets the mouse local PosX -- X local PosY -- Y local PosZ -- Z local GridSize = 2 -- Example local Model = game.ReplicatedStorage.Model:Clone() -- Model you have ready -- Main script local function Snap() --Sets variables PosX = math.floor(Mouse.Hit.X / GridSize + 0.5) * GridSize -- SNAP PosY = Mouse.Hit.Y PosZ = math.floor(Mouse.Hit.Z / GridSize + 0.5) * GridSize -- SNAP end local function Movement() Mouse.TargetFilter = Model --Ignores the model Snap() -- Gets the variables ready (We will snap later) Model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ)) -- Moves the model end -- Starts placement local function StartPlacement() Model.Parent = workspace end game.Players.PlayerAdded:Connect(StartPlacement) -- Simple so not explaining this
This is the simplest way to snap to a grid. Hope this helps (if you see this.)