I created an HUD with a canvas, and a simple image (of a sniper target). I’m trying to get the 3D position in world.
In order to do that, I’m first trying to get the 2D position of the target (it’s not always in the center of the viewport).
I looked online and found I should use widget → get cached geometry → local to viewport.
However, the results seems incorrect.
I tested it by comparing the [viewport size / 2] to the result of the above method (… local to viewport).
I tried doing that when the image center is at the viewport center (alignment 0.5 0.5) and when the top left corner of it is at the center (alignment 0 0). Either way, the position was incorrect (usually a bit more; around 10 units more, which is noticeable).
What am I missing? Is there a better way to get the screen position of the widget?
I managed to get the screen position in pixels of the top-left corner of a widget image (of a crosshair) by using the node chain you described. Here’s a blueprint I used to the shoot a debug line out from the crosshair:
Where online did you find the suggested GetCachedGeometry → Local To Viewport method? It was really helpful for me. I wonder how to dynamically get the size of the widget image so its center can be located.
Actually I’m running into the same issue. My “crosshair” is a 200 x 200 (widget units?) size image widget square. When the alignment and position are (0,0) Pixel Position returns the upper left corner regardless of the anchor. If I set the alignment to 0.5, 0.5, it does not offset Pixel Position by 1/2 of the size.
On second thought, the documentation says not to use the GetCachedGeometry Node.