I’m trying to make a grid system, but my block floats for some reason…
Video:
It also goes inside other parts… How could I fix both?
Code:
function SnapToGrid(pos)
return Vector3.new( math.round(pos.X / GridSize.Value) , math.round(pos.Y / GridSize.Value) , math.round(pos.Z / GridSize.Value) ) * GridSize.Value
end
local function renderPartPreview()
local mouseLocation = UserInputService:GetMouseLocation()
local unitRay = camera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
local cast = workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, CastParams)
if cast and partPreview then
local position = cast.Position + (cast.Normal * (partPreview.Size/2))
local snappedPosition = SnapToGrid(position)
partPreview.Position = snappedPosition
end
end
The block’s size is (0.5, 0.5, 0.5) and the GridSize.Value is also 0.5.
i tried fixing this at one point, your only solution is to either have everything like the baseplate and the blocks be adjusted to a 0.5 snap (the primary cause of the problem is your environment not being at this 0.5 snap), or you make some really sophisticated query thing that requires 5 PhDs to even create it at all
I have never gotten my post referenced to it being referenced twice in 1 week.
The main problem with snapping to world chords is that it has no care of where any blocks are at
Snapping relative to an object can just about gaurantee no clipping.
I also see that @Guest_2000123145 is working with even multiples of the gridsize. Since my solution uses the center of the face as the point
The grid will be off by about a half.
This can just always be fixed by adding like 1/2*gridsize before Rounding and subtracting 1/2*gridsize after so.
I would like to mention that this piece of code is completely fine. The Reference (tag or whatever is called) to my other solution would create a small bug anyways because I forgot that :Floor() is not the same as Rounding
My main issue is it not clipping into other blocks, I’d rather it float on top. This happens when the grid size/increment isn’t small enough for the block to be flush.
For example, the block’s size is 1,1,1, which means its Y coordinate for it to be on the ground needs to be “0.5”.
“0.5” isn’t allowed when the grid size is 0.75, 1, or 2. It’s only allowed in grid sizes that completely divide without remainder into 0.5 (does that make sense?)
Anyway, thank you guys so much, I hope this post would be useful for someone in the future! :D
So if you calculate 1/2 the Y axis height of both parts and subtract them from the Y Position you will get the bottom surface Position of each Part.
Add 1/2 the Y height of the Part being placed for its final Position. This should allow all Parts to sit flat compared to the bottom surface of the block the cursor is on.