How do I make the placement system align with parts

Games like Lumber Tycoon 2 have a placement system, and if you were to place a conveyor in Lumber Tycoon 2 on the side of another one, they would align perfectly. How can I do that with mine, NOT using surfaces or raycasting, and just the mouses target? my placement system already works just not aligning. Also my first post so it might be not that good.

TO prevent errors exactly like this
image

if I were to try to place it on the side of the conveyor, it would go inside conveyor because I don’t know how to implement the system in the title

place Code:

RunService.RenderStepped:Connect(function()
	if CurrentModel and Mouse.Target then
		local Primary = CurrentModel.PrimaryPart
		local Pos = Mouse.Hit.Position+Vector3.new(0,0,0)
		Pos = Vector3.new(
			math.floor(Pos.X / gridSize + 0.5) * gridSize,
			math.floor(Pos.Y / gridSize + 0.5) * gridSize,
			math.floor(Pos.Z / gridSize + 0.5) * gridSize
		)
		
		CurrentModel:SetPrimaryPartCFrame(CFrame.new(Pos+Vector3.new(0,CurrentModel:GetExtentsSize().Y/2,0))*CFrame.Angles(0,math.rad(CurrentRotationY),0))
	end
end)
4 Likes

GetTouchingParts and calculate the distance for alignment, then lock the part along an axis to slide the part until aligned

1 Like

how can I use GetTouchingParts to do that?

1 Like

Does anybody have solution or can expand the information on the first reply? This placement system is very important for my game to line up parts.

1 Like

Still having this problem, no solutions were found online or in the developer forum.

Can you elaborate on how you want it to “align”? I don’t understand.

For example if I was building:
image

Left is no alignment, right is alignment. That is the behavior I want, on the right. Like Lumber Tycoon 2 building alignment system.

So like alignment to the nearest touching part?

whatever part the mouse target is, is where the building that they selecting aligns to, using setPrimaryPartCFrame

Why don’t you try taking the cframe of the object and adding an offset to the x or z axis?

I am trying to make the object align to the surface of the mouses target. Like Lumber Tycoon 2. In that game you can see that when you place an object on a surface it aligns on top or in front and never inside the surface.

Ok I understand, why not use raycasting?
I see that in your title you don’t want to use raycasting but I’m wondering why? Is it because you don’t understand raycasting, if you don’t why not just learn how to use it? Whatever the reason its pretty hard to think of a way to align the parts position to on top of whatever you want without using a raycast or shapecast.

I’m not sure why you don’t want to use raycasting. It returns a normal, and an instance that it touched, it’s way easier. Is it just a pet peeve? Or for some other functional reason?

If there is a way I can use raycasting, then I will do it if it is the only way. For both you and @Den_vers I can use raycasting, I just didn’t want to use it since I have no idea how to use it on this type of thing.

If you don’t know how to use Raycasting I recommend dropping what your doing right now and going to learn it there are a bunch of tutorials online on Youtube and the Devforum ill send some links:

https://youtu.be/Wl3mXFlRPo4?si=QIY-ptpeK7UGCvpG

https://youtu.be/_l_MtYGPvCU?si=BrBVLy0iUjZyiV09

https://youtu.be/CPcaJgQkxM8?si=QUPHoKZ_wD0L5Vje

Also with the system your asking us to make, are you asking us to create you a system that simply aligns parts based on their position to each other, if so really just use raycasting

I will use raycasting. I already have created gun system and did other things with raycasting, I just am not sure for this particular problem how I can do it.

Don’t use :SetPrimaryPartCFrame(), it’s deprecated. Use :PivotTo(). If you want to get the target use mouse.Target. To align it, I would get the position of the part (assuming it’s a sphere or square) and multiply it by half the width of the target, and half the width of the part being placed. Like this:

local part = path.to.part

-- after raycast

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Include
raycastParams.FilterDescendantInstances = {} -- list of parts (and descendants) to include in search

local raycast = workspace:Raycast(
    camera.Position,
    (camera.Position - mouse.Hit.CFrame) * 10,
    raycastParams)

if raycast then
    local normal = raycast.Normal
    
    part.CFrame = raycast.Position + (normal * part.Size.X)
end

It’s a rough draft, try it out. I’m also making the assumption that part.Size.X is the axis that will be facing the other part.

I edited code some and it is levitating though

image

the code:

local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		raycastParams.FilterDescendantsInstances = {CurrentModel} 

		local raycast = workspace:Raycast(
			Camera.CFrame.Position,
			(Mouse.Hit.Position - Camera.CFrame.Position) * 10,
			raycastParams)

		if raycast then
			print("Yes6")
			local normal = raycast.Normal

			CurrentModel:PivotTo(CFrame.new(raycast.Position + (normal * CurrentModel:GetExtentsSize().X)))
		end
1 Like

Also if you only do X axis, does that mean the model is only gonna align on X axis? I would want all axis to align on. So far it aligns just offset some shown in picture above.

Yeah, you’ll have to check the rotation of the object, I’m not sure what system you have in place for that. Determine which axis to use based on the rotation.