Wrapping Points Around an Object

I want to get some points that wrap around an object, similar to how the Scale tool in Roblox Studio wraps its points around the part we select. I’ve written the following function to achieve it:

for foo, point in ipairs(points:GetChildren()) do
	local adjustment = Vector3.zero
	
	if point:HasTag("PosXPoint") then
		adjustment = Vector3.new(target.Size.X / 2 + 1.5, 0, 0)
		
	elseif point:HasTag("PosYPoint") then
		adjustment = Vector3.new(0, target.Size.Y / 2 + 1.5, 0)
		
	elseif point:HasTag("PosZPoint") then
		adjustment = Vector3.new(0, 0, target.Size.Z / 2 + 1.5)
			
	elseif point:HasTag("NegXPoint") then
		adjustment = Vector3.new(-target.Size.X / 2 - 1.5, 0, 0)
			
	elseif point:HasTag("NegYPoint") then
		adjustment = Vector3.new(0, -target.Size.Y / 2 - 1.5, 0)
		
	elseif point:HasTag("NegZPoint") then
		adjustment = Vector3.new(0, 0, -target.Size.Z / 2 - 1.5)
		
	else 
		error("Point did not have an expected tag")
	end
		
	point:PivotTo(target:GetPivot() + adjustment)
end

It works, but I’m not very happy about how much boilerplate (ie. the fact that the only difference between each case is the coordinate and the sign of target.Size.Coordinate / 2 + 1.5) I have written and I’m wondering if there’s a way to achieve the same effect with less code?

You can loop over the NormalId EnumItems and then use the Vector3.FromNormalId constructor to avoid the 6-case conditional:

for _, normalId: Enum.NormalId in Enum.NormalId:GetEnumItems() do
	local point: PVInstance = parentOfPoints:FindFirstChild(normalId.Name) :: PVInstance --PVInstance is essentially a BasePart or Model
	local normalVector: Vector3 = Vector3.FromNormalId(normalId)
	local offset: Vector3 = (0.5 * target.Size * normalVector) + (1.5 * normalVector)
	-- Parentheses added for clarity: (size relative offset) + (constant offset)

	point:PivotTo(target:GetPivot() * CFrame.new(offset))
	--This is a multiplication by a CFrame to account for rotation in the `target`
end
1 Like

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