Moving UI element with position or recttransform.anchoredposition ?

Hello everyone !

I’m discovering the UI features, and, for a drag and drop effect, I need a UI element to follow my mouse.
However, applying the mouseposition to the recttransform.anchoredposition gives weird result, either with an offset, or with a mirrored movement. Is there any reason we shouldn’t use the transform.position of UI ? Because this works perfectly and I would be enclined to use it, but I don’t want it to cause trouble down the line.

Thanks a lot :slight_smile:

1 Like

Using transform.position is fine. anchoredPosition moves the object relative to its anchors. transform.position ignores the anchors. So unless you need your object to be aligned relative to some other UI elements, you’re fine.

1 Like

Nothing wrong with using transform.position if that’s more convenient.

For the record, if using transform.position makes the object go where you want but anchoredPosition does not, that probably just means your coordinates are expressed in world space rather than local space. You could probably use InverseTransformPoint (and maybe some additional math) to get from one to the other if you wanted.

1 Like