0

I'm trying to move one of my UI gameobject (that has a render mode of screen space) to the position of another UI gameobject. I'm using the DoTween asset to help with the movement. I've currently tried:

myUIGameObject.transform.DOMove(myDestinationOnUI.transform.position, 1f)

and also

myUIGameObject.DOAnchorPos(myDestinationOnUI.GetComponent<RectTransform>().anchoredPosition, 1f)

both results yielded unwanted result in that the latter code's final position wasn't where the destination should've bee and the former code would sort of work but instead it seems like it just goes to the world space instead of the canvas spot. Not too sure where to go from here and any suggestions will be appreciated!

Sincerely,

WonderfulWonder
  • 515
  • 1
  • 8
  • 20

3 Answers3

1

DoTween plugin contains method DOLocalMove(this Transform target, Vector3 endValue, float duration, bool snapping = false) used this in my project on canvas, because DOMove doesn't give me an position i want.

npohutsa
  • 11
  • 3
0

You must disable all LayoutGroups (interferes with positions), which can only be done properly after a number of frames, otherwise the canvas will trigger a rebuild when disabling.

public RectTransform UIToMove;
public RectTransform UITarget;

IEnumerator Start()
{
    // Wait some frames
    yield return WaitFrames(5);

    // Disable all UI LayoutGroups
    foreach (var i in GameObject.Find("UI").GetComponentsInChildren<LayoutGroup>(true))
    {
        i.enabled = false;
    }

    // Tween
    UIToMove.DOMove(UITarget.position, 4f);
}

public IEnumerator WaitFrames(int numFrames)
{
    for (int i = 0; i < numFrames; i++)
    {
        yield return null;
    }
}
st4rdog
  • 93
  • 1
  • 6
-2

I had the exact same problem and as @st4rdog suggested, the problem seems to be with layouts.

After many trials and errors, I found a solution that works. I found out that getting the transform position in the start just wouldn't do the trick. So I got it when I wanted to do the animation and voila. It worked like magic.

But if you want to do a tween at the start, maybe you should go with the solution st4 suggested or just get rid of your layout elements.