- Home /
MoveTowards issue, animating ui.
So im trying to animate a UI element using movetowards - since the objects have images attached to them i cant use transform (although i've tried on two objects neither using recttransform, basically im trying to "hide this ui element when not where its relevant, and activate it, sliding it in from the side" - here's the code
private IEnumerator moveTowardsObject()
{
tempObject = GameObject.Find(animatorTarget);
while (Vector3.Distance(this.GetComponent<RectTransform>().localPosition,
tempObject.GetComponent<RectTransform>().localPosition) > 1f)
{
Debug.Log(this.name+" - "+this.GetComponent<RectTransform>().localPosition);
Debug.Log(tempObject.gameObject.name+" - "+tempObject.GetComponent<RectTransform>().localPosition);
this.GetComponent<RectTransform>().localPosition = Vector3.MoveTowards(
this.GetComponent<RectTransform>().localPosition,
tempObject.GetComponent<RectTransform>().localPosition,
animationSpeed * Time.deltaTime);
yield return null;
}
Debug.Log("Reached destination");
isAnimating = false;
yield return null;
}
/*public void moveTowardsObject() {
tempObject = GameObject.Find(animatorTarget);
//uiObjectScript = tempObject.GetComponent<UIObject>();
RectTransform toTarget = tempObject.GetComponent<RectTransform>();
gameObject.GetComponent<RectTransform>().localPosition = Vector2.MoveTowards(
gameObject.GetComponent<RectTransform>().localPosition,
toTarget.localPosition,
Time.deltaTime * animationSpeed);
//Debug.Log("");
//Debug.Log("Animating to "+uiObjectScript.gameObject.name);
if (gameObject.GetComponent<RectTransform>().localPosition.localPosition == toTarget.localPosition) {
Debug.Log("Reached destination");
isAnimating = false;
}
}*/
public void setTargetAndAnimate(string tempObjectName)
{
isAnimating = true;
animatorTarget = tempObjectName;
Debug.Log("Starting animating to "+tempObjectName);
StartCoroutine(moveTowardsObject());
}
Maybe im doing something wrong here, but as you can see from all the stuff i have commented out, i have tried various ways to do this, looked at several questions and threads on the subject- but i cant seem to make it work, in a nice and clean manner. And while i know my code, unity is a different beast.
Does anyone have a solution? Or is able to help me ? I've been working on this for hours, and surely it must be possible- it does NOT throw any exceptions or errors, it just simply does not move the elements what so ever, and the while loop in this specific attempt just continues to run. Again i've tried atleast 6 different ways so far. So i figured it was time to ask if anyone knew what was up and how to do this. - thankyou!
Still havent found any good solution.. Anyone? Edit2: so when i run this code- i tried to output the distance between to console - it gives me, that it moves a very small number - no matter what i set animation speed to ?
here is a picture of what i mean - https://imgur.com/a/FbaqhIu
Answer by Hellium · Sep 02, 2018 at 11:21 AM
Please, take a look the the documentation. UI elements should not be moved using position
nor localPosition
, but with anchoredPosition
or offsetMin
/ offsetMax
.
the UI element is specifically not anchored. Nor would that answer my question or fix it. And if not that, then what am i suppose to do?
As indicated in the documentation, For a non-stretching Rect Transform, the position is set most easily by setting the anchoredPosition and the sizeDelta properties
. So replace every occurence of localPosition
by anchoredPosition
I tried that before- didnt seem to work, however it was because my script was using a public float that i had to set in the editor, so i fixed it. - however you're correct that, that is the best way to do it. Please post this as an answer and ill set this as solved. Thankyou!