How can I move models in grid with mouse.move?

Hello devs! So, I’m trying to make a placement system I can do the placing stuff but I’m really confused at the object movement part. I want to make it in grid. I’ve tried a lot of stuff, went through a lot of posts, youtube videos but really found nothing useful.

It will be really appreciated if anybody can help me (Again I’m not asking for scripts but I’ve no idea of how to do it). Thanks for any help!

  • AridFights1

What do you have so far? What exactly is your code currently able to do, and how would you want to improve it?

Also it would help if you posted the code you had so far :slight_smile:

1 Like

Okay so I did try something, but on this it kinda works but it’s not totally working. I’ll send you the code once I go to studio

Edit: All I can say right now is on the Y axis it works smooth and on the X and Z axis it’s not totally working properly

1 Like

hmmmm, did you know about math.floor?
Also; can you show codes?

1 Like

Here’s my code so far:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Grid = 4

Mouse.Move:Connect(function()
	local X = math.floor((Mouse.Hit.X + Grid / 2) / Grid) * Grid
	local Y = Mouse.Hit.Y + Grid / 2
	local Z = math.floor((Mouse.Hit.Z + Grid / 2) / Grid) * Grid
	
	game.Workspace.Block:SetPrimaryPartCFrame(CFrame.new(X, Y, Z))
	Mouse.TargetFilter = game.Workspace.Block
end)

NOTE: I haven’t made the script by myself but yes, I have changed a few stuff.

It seems to work for me, did you test the script yet?

1 Like

It works yes, but actually on the Y axis it’s working smooth and the grid is not set correctly Try moving your object on the Y axis you’ll understand

1 Like

Yeah, try to remove

* Grid

on each math.floor and tell me if it works smooth if is; then it has to do with your Grid variable, i honestly try setting the Grid to lowest possible like 1.

I set it to 4 because I want my objects to be 4, 4, 4 (on size)

1 Like

your Y doesn’t have * Grid; you could try changing your Y variable to:

local Y = math.ceil((Mouse.Hit.Y + Grid / 2 ) / Grid) * Grid

edit(it seems odd to have 2 “/” on variable idk why.)

1 Like

Oh I haven’t thought of using math.ceil. Ima test it!

if it still doesn’t work try to completely change your variable to this.

local posX = math.floor(mouse.Hit.X / gridSize + 2) * Grid
local posY = math.ceil(mouse.Hit.Z / Grid + 2) * Grid
local posZ = math.floor(mouse.Hit.Z / Grid + 2) * Grid

it’s still weird to see double “/” so i did that
forgot posY was Z but you could change it right?

1 Like

Correction: mouse.Hit.Y

Lol

tell me if it works :grinning:

1 Like

Kinda works, I’ll try changing the values

set it to 3.5 i guess, but you could just do trial and error until you get the preferred thing you want

I’ll test after taking my lunch

1 Like

Alright so I’ve been testing quite a bit, and came up to here:

Code
local mouse = game.Players.LocalPlayer:GetMouse()

local posX
local posY
local posZ

local Grid = 4
local halfOfGrid = (Grid / 2)

local function Snap()
	posX = math.floor(mouse.Hit.X / Grid) * Grid
	posY = math.ceil(mouse.Hit.Y + 2)
	posZ = math.floor(mouse.Hit.Z / Grid) * Grid
end

mouse.Move:Connect(function()
	Snap()
	
	game.Workspace.Block:SetPrimaryPartCFrame(CFrame.new(posX, posY, posZ) + Vector3.new(halfOfGrid, 0, halfOfGrid))
	mouse.TargetFilter = game.Workspace.Block
end)

This time everything except the Y axis does work. I need some help on the Y axis.

Thanks!

Sorry if i was late; but you can change PosY to this

posY = math.ceil(mouse.Hit.Y / Grid) * Grid
1 Like

Okay, I’ll try it! :slight_smile:

1 Like