Moving between 2 UI objects

Hi!

I have two UI objects. They are in different hierarchies. I want to move from one object to another. Moving is a simple operation. All you have to do is to calculate the direction vector and change the coordinates over time by this vector. However… What coordinates ? UI object coordinates is a RectTransform coordinates. I tried to change the anchoredPosition/anchoredPosition3D but that not working. These two objects should be in one coordinate system. But as far as I understand anchoredPosition is a local bounded. Tried using ‘.position’ but that is related to camera. I wonder if it is so difficult really ?

How to move two UI objects placed in different herarchies ?

Thank you in advance.

I have the same problem currently.

I try to move one object to different positions, given a RectTransform for all the targets.
But as in your case, my target positions needs to be in another hierarchy as my starting object, which itself is (with several other objects) under another RectTransform. Both of the hierarchy dividing elements are under the same canvas though…
So there really must be a working solution. I couldn’t find it yet.

I fixed the problem using global position: RectTransform.position. But this is not a good solution in my opinion, because as long as you move your UI camera the constants that you use for your movements should be changed also.

I’ll give an example. I move some object from point A to point B. And I want to add some fluctuations during movement. I set AnimationCurve, and set parameter: ‘Amplitude’ that shows the force of fluctuations. So you will set up the constant as you need. And when you will need to change UI camera zoom, for example, you will probably need to change this constant as well, because global position it is position not in screen coordinates, but in global.

Even with a static camera in my setup, the .position value doesn’t seem to give me the actual position on the canvas. Might this be due to the canvas scaler? The distance I am getting out of start and target position ist bigger than the actual distance between the objects visually, and the current scale of the auto-scaling canvas is 1.255.
Does this mean I can only translate UI elements accurately, when I fix the screen size, making the app not usable for varying screen sizes, or do I really have to divide every global position I receive of any RectTransform by the current canvas scale? Sounds troublesome.

I guess the same thing might apply to you, then? That you always have to track these values and consider them in your translation formula? I am not entirely sure how your movement is resolved. You want a “straight” line with some “random” offsets along the way?

Try to move by a global position.

Vector3 dir = toPos - fromPos;
Vector3 fluctuation = new Vector3(m_mainCurve.Evaluate(normalizedTime) * Amplitude, 0.0f);
object.position += dir * (Time.deltaTime / FlownTime) + fluctuation;

My problem is in fluctuation that depends on a camera position. But if the camera is in fixed position there is no problem.