Problem with Converting Mouse Position from Local to Viewport

Hi,

I’m currently trying to convert my mouse position from a widget’s local position to a world location, but I’m encountering an issue where the Y-axis is offset from the expected position.

I have two Images inside a Canvas Panel, splitting the entire screen in half. Each Image contains a Render Target that renders the whole scene. My goal is to convert the mouse position from the Image to a world location.

I followed the approach described in this Reddit post, which provides a method to normalize the mouse position within the widget, giving values in the range (0, 0) to (1, 1).

After I got the position,I multiply the normalized values by the viewport size to get the absolute screen position. Then I use the Deproject screen to world function from controller to get the world location and direction for sphere trace.

The problem arises when moving the mouse to the top or bottom. The tracing result appears correctly in the main viewport, but the corresponding position in the Render Target is offset, especially on the Y-axis.

For example, when the mouse is at the center of the widget, the deprojected world position aligns as expected:


At this point, it is return the (0.5, 0.5) location of the image, and project to viewport, it should be in the middle as well showed in the image. That is the behaviour I want.

However, when moving the mouse to the top-left corner, the trace result in the main viewport is correct, but the position in the Render Target still appears near the center instead of aligning with the expected world location.

Here is the top left of the corser. You can see that

The Render Target’s width is set to Viewport Width / 2, and the height matches the viewport height (since the screen is split between two images).

Here is the code example I used:


This will give the 0 to 1 of x and y for mouse position and I multiply with viewport size then it will give the position accroding to viewport.

Then when we got the viewport position, I project to world location and direction.

Then I make a sphere trace from it. And result the above image.

If you have any insights or relevant resources, please let me know. Thanks in advance!