Set CFrame relative to Screen Size

I’m creating a minimap and am trying to set the minimap to always appear on the top right corner of the game.

My minimap is done by attaching a surface gui to a workspace part and having a viewport frame within the surface gui.

image

I’m currently trying to set the Cframe of this workspace part such that it appears on the top right corner.

--position of the minimap on player's screen
 minimap:SetPrimaryPartCFrame(game.Workspace.CurrentCamera.CFrame*CFrame.Angles(math.rad(90), 0, 0)*CFrame.new(10,-10,-3))

However, this method is not responsive to screen size. When my screen is wide, the position of the minimap is as such:

but when my screen becomes narrower, it’s as such:

is there any way I can position my minimap to always be at the top right of the screen?

Any reasons why you are using a surface gui with a workspace part? Are you looking to make a 3d map like effect? If not then a ScreenGui like in the Frames | Roblox Creator Documentation tutorial should be suitable enough for 2d maps.

If so then to make it relative to screen size you will definitely to make the part relative to a Udim2 as Udim2’s scale with screen size automatically. I believe you can create a GUI Frame | Roblox Creator Documentation for this, set the Udim2 to position on the screen the usual way, then work with the GuiBase2d | Roblox Creator Documentation property and position properties somehow, I’m not too experienced with this unfortunately.

I’m creating a 3D map effect, so I’m using the surface gui with a workspace part!

I had to do something similar as this for a game and I forgot what was the exact cframe code but it was basically ToObjectSpace but on a low level (iirc it was like)

part.CFrame = camera.CFrame + CFrame.new(camera.CFrame.LookVector * 5) + CFrame.new(camera.CFrame.RightVector * 5)

I would use Camera | Roblox Creator Documentation or Camera | Roblox Creator Documentation to convert a 2D position into a 3D ray that starts at the depth given away from the camera.

Use its Origin as the position of your CFrame.