I’m working on a placement system of a new sandbox tycoon game I’ve been working on over the past week, and progress has been going really well for how long I’ve been away from lua. Anyways, I’ve been facing a problem with the placement system in recent times.
Items are supposed to snap to the base in equal increments and are supposed to line up with the edge of a base.
It’s very difficult to explain in text so here’s an image of what happens, and what I need it to do:
There fortunately have been some “recent” updates to the engine that make this type of job pretty simple to do.
In your case, the easiest method would be to use Model.PrimaryPart and Model:SetPrimaryPartCFrame()
Here’s the wiki pages:
My most simple explanation for this is that once you assign the PrimaryPart variable of a model, you can then use Model:SetPrimaryPartCFrame(cframe argument), to move the model based off the PrimaryPart’s cframe.
Also Mouse functions are er… slightly outdated, UserInputService is a pretty neat alternative if you’re looking to dive into some newer features.
Here’s the wiki link
However, the mouse functions still work just fine.
Well first things first, you’re going to want to make sure your models are sized according to the grid, well the base of the model anyways, For example if your grid is a 10x10 grid of 2x2 squares you will want to make the x and z size of the base of the model a factor of 2.
Secondly, You will want to try do something as shown:
You will want to run a check to see if the math.floor() x and z values of the mouse are a factor of your grid cell size. For example your mouse is positioned at 0.5 on the x axis, and your cell grid size is 2, It will run a check as follows:
local topleft = Grid.Position - Vector3.new(grid.Size.X / 2, 0, Grid.Size.Z / 2)
local StudsOutInVector = flooredposofmouse - topleft
if StudsOutInVector.X % 2 = 0 and StudsOutInVector.Z % 2 = 0 or StudsOutInVector.X = 0 or StudsOutInVector.Z = 0 then
local CFrameMath = CFrame.new(Grid)
model:SetPrimaryPartCFrame((topleft + StudsOutInVector) + Vector3.new(Model.Base.Size.X / 2, 0, Mode.Base.Size.Z / 2))
end
thank you for your response! Sorry for the bad clarification on my end, I should have mentioned I had already created the grid snapping, however it was not perfectly snapping to the grid like I had wished (seen in imgur links)
I was looking at world/object space thinking maybe that would help?
Sorry small fix again, and if you can, you want to try construct a new vector by doing Vector3.new(mouse.hit.x floored, mouse.hit.y, mouse.hit.z floored) you set flooredposofmouse to the new vector you constructed
Also the if statements can be tidied up also im sure but thats the basic way of doing it.