How can I add collisions to my placement system?

I’ve made a placement system, and I’m stuck on how to make a collision system. I have got a system where the part turns red if it’s colliding, and green if not but instead I want it so the part snaps to the surface it’s on.

At the moment parts go halfway into the surface they are on. I made a quick fix for the Y by adding 0.5 to it, but that doesn’t work most of the time. I have tried GetPartsInPart, but with what I tried the part would stay in the same spot for the whole time.

I have looked around on the devforum and google, but nothing has helped me.

As you can see, on the baseplate the part is fine, but on the wall it goes halfway into the part, and on top it is floating above it.

function build(item)
	local part = workspace.MainFolder.BuildItems[item]:Clone()
	part.Name = "Build"
	part.Parent = workspace
	local Mouse = game.Players.LocalPlayer:GetMouse()
	local hitp = Mouse.Hit.Position.Y
	local gridSize = 1
	local db = false
	game:GetService("RunService").RenderStepped:Connect(function()
		Mouse.TargetFilter = part
		part.Transparency = 0.5
		part.CanCollide = false
		part.Anchored = true
		
		part.Position = Vector3.new(math.floor(Mouse.Hit.X / gridSize + 0.5) * gridSize, math.floor(Mouse.Hit.Y / gridSize + 0.5) + 0.5 ,math.floor(Mouse.Hit.Z / gridSize + 0.5) * gridSize)
		
		local collisions = workspace:GetPartsInPart(part)
		
		if #collisions > 0 then
			part.BrickColor = BrickColor.new("Really red")
		else
			part.BrickColor = BrickColor.new("Neon green")
		end
		
		Mouse.Button1Down:Connect(function()
			if db == false then
				db = true
				local clone = part:Clone()
				clone.Name = "Clone"
				clone.Parent = workspace
				clone.Transparency = 0
				clone.CanCollide = true
				clone.Position = part.Position
				clone.Anchored = true
				wait(0.1)
				db = false
			end
		end)
	end)
end

That script gets fired when a player equips a resource to build with.

In terms of collisions, have you tried collision groups?

I haven’t heard of those before. I’ll look into it

I had a look, and that looks like the solution but how would I add that into the code?

There is a collision group editor in the model section in roblox studio. There you can create “groups” and select which other groups they can and cant collide with. by default every part that exists is in the “Default” group i think. In terms of scripting, .CollisionGroup is a property of Parts. So I would maybe make a collision group called “Selection Part” or wtv you are calling the highlighted part you are placing and and make it so it can’t collide with default parts. and then when it places, just change that property to be default, or however else you are handling the placing of blocks.

Yeah, that’s what I mean. And also on the ground / on flat surfaces it also is on top.

I tried that, but it didn’t work. I made a group called “Selection Part” and made it so it could collide with “Deafult”, and in my code I made the parts collision group “Selection Part” but it still went halfway into the part.

If I’ve read the post correctly, you’re having trouble with the collision and surface placement aspects of the system. For the object to be correctly placed on the side, you’ll need to use normal vectors. You can use Raycasting.

As for the collision, you may use the function object:GetTouchingParts() and check if the tables length is greater than one:

if (#object:GetTouchingParts() > 1) then
	-- this means you can't place the object.
	-- do what you gotta do here like:
	-- object.Color = Color3.fromRGB(255, 0, 0) -- red

	return
end

If don’t understand, you can follow a tutorial on the devforum (by EgoMoose) or a multi-part series (by zblox) There are many tutorials out there, just find one which works for you.

Shoot a raycast from camera position to mouse 3D position and place the part in that position by doing so:

part.Position = raycastResult.Position + raycastResult.Normal * part.Size / 2

That moves the part half it’s size on whatever axis it’s getting placed on.

I think the issue is that the collision groups wont help if you are setting the position of the object to a specific coordinate. then collision groups wont work. but like @Inpultion said, using raycasting might come in handy. You can shoot rays in each direction and if it hits then you can use the normal vector to calculate which position to set it to in order to make it “snap”

1 Like

Thank you for the help, this topic has made me realise that this is above my skill level and I will try using a tutorial so then I can understand it better.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.