Why is my block floating?

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.

Thanks!

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

Use the Search tool up top using the term grid placement . There are lots of useful posts about this topic already, just with a different title.

maybe also divide by the part’s Size.Y / 2.

math.round(pos.Y / GridSize.Value / (partPreview.Size.Y / 2))


that only makes it worse lol
but thanks for trying :D

Wait im pretty sure i did the math wrong

math.round(pos.Y / GridSize.Value) - (partPreview.Size.Y / 2)

not sure if that works


there’s still a small gap

Maybe try removing dividing by 2? It looks like its getting closer.

math.round(pos.Y / GridSize.Value) - partPreview.Size.Y

else im off ideas


That does get rid of the issue in some places, but then reintroduces it elsewhere…

I searched grid placement like I stated before and got these within the first 15 search items:
Help with grid placement
GridPlacer: Highly-featured module for part placement, rotation, and snapping to grid
When would it be useful to use the modulus operator (fmod() or %)? - #4 by handsomepoliceman22
Need help with some Complex CFrame math
How would i be able to make a part snap to a grid
How can I make a surface grid building system? - #33 by apictrain0
Guaranteed you’ll get an answer from some of these solved posts instead of just guessing what works.

1 Like

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 :smile:

2 Likes

Alright, this is what I’ve come up with:

Basically lines telling you were the grid is.

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.