Are ProjectWorldToScreen and DrawLine using different coordinate systems?

I want to draw lines to indicate relationships between actors on screen, for example unit movement paths. I have a User Widget and in OnPaint I’m using ProjectWorldToScreen to get screen space coordinates for the world positions and then Draw Line to draw the actual line. The length and direction of the line I get is correct, but it’s position is off by quite a bit, and the offset varies depending on the size of the render window.

What do I need to do to convert the ProjectWorldToScreen result to a coordinate that can be passed to Draw Line to get a line that start and end right on top of the world position I give to ProjectWorldToScreen? If no possible, what are some other options to accomplish the same thing?

Some images showing my setup, based on the TopDown template, and the result.

Additions to the TopDownCharacter Blueprint class.

OnPaint in my User Widget Blueprint. The widget contains no sub-widgets.

The result I see in-game.

Animated version.

303486-screen-space-line-game.gif

Can you make the game window fullscreen, just to test it?

Can you make the game window
fullscreen, just to test it?

If that produces desired result, try converting it like so:

I’ve been doing some reading in the UMG part of the documentation, in particular about scaling. The problem is indeed that Project World to Screen and Draw Line uses different screen coordinate systems. We can convert between them by dividing by or multiplying with Get Viewport Scale.

There is also Project World Location to Widget Position which does that for us. I get the line I want when I replace Project World to Screen with Project World Location to Widget Position.

While not explicitly stated in the user manual, at least that I could find, I got some hints from
Scale UI for Different Devices | Unreal Engine Documentation and
DPI Scaling | Unreal Engine Documentation, which along with the ever useful search box that appears when right-clicking in a Blueprint helped me find the nodes I needed.

1 Like

Saved my life, thanks so much!!!