Getting the 2D Screen position of a Viewport object

I am working on a boardgame which uses a viewport for the die-rolling visuals. When the dice lands, I want a number to appear above it (as a UI). This is easy enough to do on one screen size, but because I want to support a variety of screen sizes, I’d like to be able to accurately find the screen position of the die object, which I currently do not know how to do.
I’ve tried using WorldToViewportPoint and WorldToScreenPoint (using viewport camera and die position) but they did not seem to work as intended for viewports.
If anyone has any idea on how to implement this, please let me know :slightly_smiling_face:

Example image for Mobile


Example image for PC

Looks fine on mobile but not on PC.

1 Like

UI has a property called absolute position, which takes the screen size into account.

and here is how you would take the viewports absolute positions and tell where the number should go.

1 Like

The viewport size for cameras for a viewportframe is 1,1 when using WorldToViewportPoint and WorldToScreenPoint.

So here’s a way to convert it to something usable:

3 Likes

That works great and is exactly what I need. Thank you.

1 Like

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