1
\$\begingroup\$

UI diagram

I want to achieve the following:

At some point in the game, the UI Image (dashed rectangle) should shift towards the right edge of the screen (black outline rectangle) up until the part of the image, marked with Helper Object, reaches the right edge of the screen.

What I tried:

Helper Object's localPosition seems always to be in the "Canvas" coordinates (I have a 4k reference resolution, 3840x2160)

So, first I calculate B by dividing the canvas reference resolution by 2 and subtracting HelperObject's x position:

var distanceToMoveCanvas = _canvasScaler.referenceResolution.x / 2f - _helperObject.localPosition.x;

Then, I'll calculate what percentage is B of totalWidth:

var distanceToMovePercent = distanceToMoveCanvas / _canvasScaler.referenceResolution.x;

And then, I'm calculating what is this distance in "screen space":

var distanceToMoveScreenSpace = Screen.width * distanceToMovePercent;

Now, I've tried adding each one of these values to position, localPosition, anchoredPosition x coordinate of the UI Image but nothing was correct.

How can I achieve this?

\$\endgroup\$

0

You must log in to answer this question.

Browse other questions tagged .