3D world position to 2D screen position

Hello developers! So I am working on my Interactions system (No I don’t want to use ProximityPrompt) and I want to recreate one from game SCP : Containment Breach, I know you are now smacking your head while saying “He could just use BillboardGui” trust me, It won’t look like SCP : Containment Breach, what I mean is that no matter if you are looking at the interaction part or not, the press E thingy is still on screen, and I need it to position based on the part’s 3D position, thanks for any help!

There are specific API methods for this.

https://developer.roblox.com/en-us/api-reference/function/Camera/WorldToViewportPoint
https://developer.roblox.com/en-us/api-reference/function/Camera/WorldToScreenPoint

The former ignores the core gui inset and the latter acknowledges it.

4 Likes

Thanks! One more question, how would I set the ImageLabel’s position to offset instead of scale?

Both of those functions return a Vector3 value of which its X & Y properties describe a pixel offset from the top-left corner of the screen.

I know, but when I set ImageLabel position to Offset instead of Scale, It doesn’t show on screen, I tried doing some math, that didn’t work out.

Can you share the line where you assign the GuiObject’s position property?

Wait I asked wrong thing, I wanted to say, how would I set it to Scale instead of Offset, my bad.

Oh, you’d divide the result by the viewport’s dimensions, i.e;

local scaleX = result.X / camera.ViewportSize.X
local scaleY = result.Y / camera.ViewportSize.Y

Make sure this is being handled locally.

The server’s camera has a viewport size of 1 by 1 pixel.

1 Like

Thanks, I am programmer for 5 years and still forget some stuff.

Oh and one last question xd, how do I prevent it from scaling the image?

Insert a UIAspectRatioConstraint into the ImageLabel/ImageButton.

https://developer.roblox.com/en-us/api-reference/class/UIAspectRatioConstraint