How do I make a part snap to a wall

I’m trying to make a building system, and I am using raycast normals to offset the building part. The problem is, when the object is rotated, it gets all messed up.

This is how it looks like unrotated, nothing wrong

This is how it looks like when it is rotated on multiple axis, it is all messed up. It is going through the wall.

Someone please help me, I cannot find a way to fix it, I searched everywhere, thanks if you can help.

2 Likes

Could you show the code, also your trying to rotate on every axis?

2 Likes

This is the main mover

local mouseray = mouse.UnitRay
			local CastRay = Ray.new(mouseray.Origin, mouseray.Direction * 1000)
			local hit, position, normal = workspace:FindPartOnRayWithIgnoreList(CastRay, ignorelist)
			local offset = Functions.getoffsetbynormal(Yrot, viewbuild.PrimaryPart, normal)

			posX, posY, posZ = Functions.calculatePos(position, posX, posY, posZ, viewbuild)
			local gridpos = Vector3.new(posX,posY,posZ)

			viewbuild:PivotTo(CFrame.new(gridpos + ((offset * .5) * normal))*CFrame.Angles(viewbuild.PrimaryPart.Orientation.X, 0, viewbuild.PrimaryPart.Orientation.Z))
			viewbuild:PivotTo(CFrame.new(gridpos + ((offset * .5) * normal))*CFrame.Angles(viewbuild.PrimaryPart.Orientation.X, math.rad(viewbuild.PrimaryPart.Orientation.Y + Yrot*90), viewbuild.PrimaryPart.Orientation.Z))

and this is the normal calculator offsetter Module

module.calnum = function(num)
	return num % 2 == 0
end

module.getoffsetbynormal = function(rotation, object, normal)
	if module.calnum(rotation) then
		return object.Size
	else
		return Vector3.new(object.Size.Z, object.Size.Y, object.Size.X)
	end
end
1 Like

Is it possible for you to help me with this?

and yes it is on every axis. I could not figure it out

1 Like