Ive made a simple sandbox placing thing, but now I have a problem, how do I stop the green block going inside? https://gyazo.com/72e52379f548b38c93402b7ee99b074e This is my block placing script down below:
SetPrimaryPartCFrame(CFrame.new(PlrMouse.Hit.p.X-PlrMouse.Hit.p.X%5+2.5, PlrMouse.Hit.p.Y-PlrMouse.Hit.p.Y%5+2.5, PlrMouse.Hit.p.Z-PlrMouse.Hit.p.Z%5+2.5))
I added +2.5 to make it in the center of the mouse also.
Instead of using CFrame
to position the model, use Vector3
because it has collision detection (will make sure that the model/part doesn't overlap with another). In order to do this, you can use the method MoveTo()
. This means your code would look like this:
--Variables used for the sake of readability local x = PlrMouse.Hit.p.X - PlrMouse.Hit.p.X % 5 + 2.5 local y = PlrMouse.Hit.p.Y - PlrMouse.Hit.p.Y % 5 + 2.5 local z = PlrMouse.Hit.p.Z - PlrMouse.Hit.p.Z % 5 + 2.5 model:MoveTo(Vector3.new(x , y , z))
I would also suggest you use a data structure of some sorts like a 3D array to represent the voxel based placement system you have so that you can represent each block that has been placed down and its corresponding state.