Make placement system stick to surfaces

  1. What do you want to achieve? Keep it simple and clear!

Basically im making a placement system.

  1. What is the issue? Include screenshots / videos if possible!

I need the system to be able to stick to different surfaces if that makes any sense. Atm you can move the objects in a grid but you cant build on top of other blocks :frowning:

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Some years ago when i didnt know about Object/World space i used to have this line of code in my systems:

local S = {
	[Enum.NormalId.Top] = Vector3.new(0,1,0),
	[Enum.NormalId.Bottom] = Vector3.new(0,-1,0),
	[Enum.NormalId.Left] = Vector3.new(-1,0,0),
	[Enum.NormalId.Right] = Vector3.new(1,0,0),
	[Enum.NormalId.Front] = Vector3.new(0,0,1),
	[Enum.NormalId.Back] = Vector3.new(0,0,-1)
}

local Surface = S[Mouse.TargetSurface]

CFrame.new(0,0,0) + (Surface * (Object.PrimaryPart.Size*.5))

I tried that with my new system which converts the Mouse position to object space. But it just doesnt work very well.

Anu help would be really cool since ive ran into a wall and dont know how to fix this issue.

Heres a vid showcasing the issue(As you can see from the video. I cant place cubes on different surfaces in this case the top)

And heres also the code i use in my placement system

local function Convert(CF,S)
	return CFrame.new(
		math.round(CF.X/3)*3,
		S.Y/2,
		math.round(CF.Z/3)*3
	)
end


Mouse.Move:Connect(function()
	
	if Mouse.Target ~= nil then
		
		Mouse.TargetFilter = Object
	
		local Rel = Plot.CFrame:ToObjectSpace(Mouse.Hit)
		local Grid = Convert(Rel,Object.PrimaryPart.Size)
		
		
		Object:PivotTo(Plot.CFrame:ToWorldSpace(Grid))
		
	end
	
end)

Could please add some prints of the values set so we can help you more.

1 Like

What do you mean by “prints of the values”?

Print out value of Rel and Grid and the PlotCframe before and after doing the Pivot.

Alr sure?

Relative CF


Grid CFf

Plot CF

Would you please print out the plot CF value before and after it is set.
as in
print(“plot before=”,Plot.CFrame) before its changed
and
print(“plot after=”,Plot.CFrame) after

instead of using Mouse.Hit, use raycasting. And don’t use Mouse.Move as your object’s position won’t update if you move your character without moving your mouse.