Need help with plugin that rotates parts

I am working on a plugin that allows you to rotate a part without its shape changing (i.e. its bounding box stays the same) since it would be helpful when dealing with cylinders and wedges. It almost works, but it ends up breaking when rotating something on a certain face.

The plugin works by putting a SurfaceSelection on the face of the part that the mouse is currently hovering over. When the mouse clicks, the direction of the face’s normal is multiplied by 90 and is added unto the parts current Orientation.

local mouse = plugin:GetMouse()
local faceSelection:SurfaceSelection? = nil

mouse.Button1Up:Connect(function()
	
	local Target = mouse.Target
	
	local v = Vector3.FromNormalId(mouse.TargetSurface)
	Target.Orientation += v * 90
	if v.X ~= 0 then
		
		Target.Size = Vector3.new(Target.Size.X, Target.Size.Z, Target.Size.Y)
		
	elseif v.Y ~= 0 then
		
		Target.Size = Vector3.new(Target.Size.Z, Target.Size.Y, Target.Size.X)
		
	elseif v.Z ~= 0 then
		
		Target.Size = Vector3.new(Target.Size.Y, Target.Size.X, Target.Size.Z)
		
	end
	
end)

LockRotate.Click:Connect(function()
	
	if plugin:IsActivated() then
		
		plugin:Deactivate()
		if faceSelection then faceSelection:Destroy() end
		
	else
		
		plugin:Activate(true)
		faceSelection = Instance.new("SurfaceSelection", workspace.Camera)
		while task.wait() do
			
			faceSelection.Adornee = mouse.Target
			faceSelection.TargetSurface = mouse.TargetSurface
			if not plugin:IsActivated() then
				
				faceSelection:Destroy()
				break
				
			end
			
		end
		
	end

end)

It seems to happen when the parts Orientation goes from -90, 0, 0 to 0, -180, -180. Help with the problem and feedback on the current code would be greatly appreciated (I’m very new to plugin development).

2 Likes

For context, the LockRotate variable refers to a button in the plugin toolbar.

1 Like

Maybe you could use Handles?

2 Likes