Need Help with Snapping a Piece to a Wall in My Roblox Dungeon

Hi everyone,

I’m working on a dungeon project in Roblox and encountering a challenge with positioning a piece accurately against the walls of my dungeon. I’m aiming to create a “green zone” that should snap perfectly to the walls of a model, but I’m running into issues where the green zone either overlaps with the model or doesn’t align as expected.

Here’s a summary of the problem:

  1. Objective: I need to position a green zone (a part) so that it snaps perfectly to the walls of a model. The green zone should be aligned with the surface of the walls and fit seamlessly into the model.
  2. Current Approach:
  • I’m using a bounding box to calculate the dimensions and position of the model.
  • I cast rays in different directions to detect the closest wall and its surface normal.
  • Based on the detected wall, I attempt to move the green zone to align with that surface.
  1. Issues Encountered:
  • The green zone sometimes overlaps with the model or does not align correctly with the wall.
  • The green zone can end up in an incorrect position, even though it’s supposed to snap precisely to the surface of the wall.

Here’s the empty space

Could perfectly fit in

Checking the piece selection

Zone code :

local function fillVoid()
		for i = 1, size.X / 20 do
			for j = 1, size.Z / 20 do
				local newCloneCube = cloneCube:Clone()
				newCloneCube.Position = Vector3.new(cloneCube.Position.X + ((-i + 1) * 20), cloneCube.Position.Y, cloneCube.Position.Z + ((-j + 1) * 20))
				newCloneCube.Parent = game.Workspace
				local parts = workspace:GetPartsInPart(newCloneCube, param)

				if #parts > 0 then
					newCloneCube:Destroy()					
				end 
				
				--local AddZone = CreatePart(Vector3.new(StartingCube.Position.X + ((-i + 1) * 20), StartingCube.Position.Y, StartingCube.Position.Z + ((-j + 1) * 20)))
			end
		end
		
		print("X : " .. size.X )
		print("Y : " .. size.Y )
		print("Z : " .. size.Z )
		
	end
	
	fillVoid()

3 Likes

Edit your code to add to the position where it would snap

What do you mean so after my analysis I noticed that the problem is because of the union piece at the bottom that is here


Here’s the thing ChatGPT explained to me which makes it sense

Union parts in Roblox are a combination of multiple parts, and sometimes the collision boundaries (the so-called “hitboxes”) of these unions are slightly offset or less accurate than regular parts. This can result in the following behaviors:

  1. Phantom collisions: The engine detects collisions with the union part, even though it visually appears that there is no intersection.
  2. Inconsistent hitboxes: The union part might have a slightly larger or smaller hitbox than expected, leading to undesired collision detections.

Then after he said

Set CollisionFidelity on Union: Each union part has a CollisionFidelity property, which determines how accurately collisions are calculated. The default value may not be sufficient for precise interactions, so try setting it to a more accurate mode, such as:

unionPart.CollisionFidelity = Enum.CollisionFidelity.PreciseConvexDecomposition

which unfortunately didn’t work since it throw the error

The current thread cannot write ‘CollisionFidelity’ (lacking capability Plugin)

So I tried this :
Capture d’écran 2024-09-05 214607

Didn’t work either

You could use shapecast, instead of raycast. Shapecast the green zone on the X axis to the left and to the right, see which side has a result (if both have a result, take shortest), and move the zone by using the resulting vector. Then do the z axis.

How do you use that interesting never heard of this concept ?

You take the object, a vector and RaycastParams. It’ll raycast the whole object.

Is it accurate touching detection because union parts seems to have an issue with the bounding box unlike part ?

Should be. Also, you can set our CollisionFidelity to ConvexHull for a more accurate collider.

I already tried this sadly didn’t work but I’ll try the shapecast

1 Like

I mean, if the collider is wrong, shapecast probably won’t work either. I assume rays intersect with the collider, not the geometry.

Ok but why there’s a problem when the zone is near union and not touching it at all but it’s not a problem where the touching object is a part then ?

Oh alright thanks man I’ll check a tutorial on rays

I found the issue in my code it’s this line

local minBound = Vector3.new(math.huge, math.huge, math.huge)
	local maxBound = Vector3.new(-math.huge, -math.huge, -math.huge)

	for _, part in ipairs(model:GetDescendants()) do
		if part:IsA("BasePart") then
			local partMin = part.Position - part.Size / math.huge
			local partMax = part.Position + part.Size / math.huge

			minBound = Vector3.new(
				math.min(minBound.X, partMin.X),
				math.min(minBound.Y, partMin.Y),
				math.min(minBound.Z, partMin.Z)
			)
			maxBound = Vector3.new(
				math.max(maxBound.X, partMax.X),
				math.max(maxBound.Y, partMax.Y),
				math.max(maxBound.Z, partMax.Z)
			)
		end
	end

	-- Expand the bounds slightly to ensure the outline is outside the piece
	local expansionOffset = Vector3.new(0.75, 0.5, 0.75)  -- Adjust this value as needed
	minBound = minBound - expansionOffset
	maxBound = maxBound + expansionOffset

	local size = maxBound - minBound
	local center = (minBound + maxBound) / 2

How can I know if ray of union intersect with the Green Zone ??

This is a difficult problem that I’m not sure you have the skills to solve, and for me it would take quite a while to solve. There’s easier ways to do what you want to do… You could work with a grid, where every part is always 1x1 cell size. That way you can just say a cell is full or empty.

I’m not sure if I fully understand your request, but it seems like you want to position an object perfectly aligned with the wall. Would it not be sufficient to place a target object, which is invisible and non-collidable, in that exact position, and then move the green zone to match the position of your target object when needed?

Sorry I came back hum basically I want to make a zone in which it will be useful for the compatiblity piece of my dungeon and the green zone would base off of a region however problem is when I put a green box it destroys because it detects that by the method it’s in a part but from my human visual perspective it’s a green zone placement perfectly valid

Shit I think the corners of the piece will help me solves my union collision problem man although I’m not 100 % sure this is a proof solution but I will definitely give it a try !!!


It didn’t include the union part in the corners man

Seems very legit now let’s try to implement it overall I used this thread to help me

how to find corners in a part