How can i get 2D position from a 3d part for a gui but with viewportframe camera?

hello, i’m trying to get the position of some parts to convert them into 2d positions and that it take the place of the screws of my ventilation hatch or whatever it is called

this is with the current camera

and when i make it with the viewport frame the gui’s appears in top left of the screen
image

how can i put the gui’s in the screws with the viewportframe camera?
image

local vpfcamera = Instance.new("Camera", viewportframe)
							vpfcamera.CFrame = campart.CFrame * CFrame.new(0,-.45,0)
							vpfcamera.Name = "vpfcamera"

							viewportframe.CurrentCamera = vpfcamera
							
							for _, tornillos in pairs(worldmodel:GetChildren()) do
								if tornillos.Name == "Screw" then
									local wpoint = tornillos.Position

									local screenpos, onscreen = vpfcamera:WorldToViewportPoint(wpoint)
									if not onscreen then
										return
									end

									local button = Instance.new("ImageButton", viewportframe.Parent)
									button.Name = "ScrewGUI"
									button.Visible = true
									button.AnchorPoint = Vector2.new(.5,.5)
									button.Size = UDim2.new(0,32,0,32)
									button.Position = UDim2.new(0,screenpos.X, 0, screenpos.Y)
2 Likes

The 2D position of a 3D object is calculated by left-multyiplying the camera view matrix with the 3D position. point = CFrame*Vector. To undo this, you just have to multiple both sides by the inverse: Vector = CFrame:Inverse()*Vector. So you need to get the ViewportFrames cframe and apply this formula.

2 Likes

I’m trying this now and realizing its more annoying than I thought, I will get back to you with a demo.

1 Like
4 Likes