UI Object not going back to original position

Hi Guys,

I’m trying to get a UI object (a panel) to move from one place to another, and then return to it’s original position afterwards. I’ve created a IEnumerator to do this. It works, but when it tries to go back to it’s original position, that tends to be miles away, even though everything else in the game is motionless, but that object.

Here’s the code I wrote.

    private IEnumerator MoveSequence(GameObject obj, GameObject[] endPos, float _duration, float? _fadeTime = null, float _moveInterlude = 0)
    {
        obj.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 135);
        Image img;
        Vector3 origPos = obj.transform.position;
        Vector3 incrPosition = obj.transform.position;

        if(!(img = obj.GetComponent<Image>()))
        {
            Debug.Log("TrainingHandler.MoveSequence - No Image component in " + obj + "!");
        }

        for (float t = 0; t < _duration; t += Time.deltaTime)
        {
            float dt = t / _duration;
            int alpha = (int)(dt * 255);
            img.color = new Color32(255, 255, 255, (byte)alpha);

            yield return new WaitForFixedUpdate();
        }

        for(int i=0; i < endPos.Length; i++)
        {
            incrPosition = obj.transform.position;
            for (float t = 0; t < _duration; t += Time.deltaTime)
            {
                float dt = t / _duration;
                obj.transform.position = Vector3.Lerp(incrPosition, endPos[i].transform.position, dt);

                yield return new WaitForFixedUpdate();
            }

            incrPosition = endPos[i].transform.position;
            yield return new WaitForSeconds(_moveInterlude);
        }

        for (float t = 0; t < _duration; t += Time.deltaTime)
        {
            float dt = t / _duration;
            float reverse = 1 - dt;
            int alpha = (int)(reverse * 255);
            img.color = new Color32(255, 255, 255, (byte)alpha);
            yield return new WaitForFixedUpdate();
        }

        obj.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 135);
        obj.transform.position = origPos;
    }

The code calling it is in the same class, and is only this:

private float BeginMoveSequence(GameObject obj, GameObject[] endObj, float _duration, float? _fadeTime = null, float _moveInterlude = 0)
    {
        float t = (_duration * endObj.Length);

        if (_moveInterlude > 0)
            t += (_moveInterlude * endObj.Length);

        if(_fadeTime != null)
        {
            t += ((float)_fadeTime * 2);
        }
        else
        {
            t += (_duration * 2);
        }

        StartCoroutine(MoveSequence(obj, endObj, _duration, _fadeTime, _moveInterlude));
        return t;
    }
public GameObject finger;
private float duration = 1;

GameObject[] moveSeq = new GameObject[4];
        moveSeq[0] = arrowLeft.gameObject;
        moveSeq[1] = arrowRight.gameObject;
        moveSeq[2] = arrowUp.gameObject;
        moveSeq[3] = arrowDown.gameObject;

        yield return new WaitForSeconds(BeginMoveSequence(finger, moveSeq, duration));

Any ideas?

Thanks!

Found the answer! :smile:

The script was being called at the beginning of play… and the origPosition was collected after the UI adjusted to the camera’s position in the world…

didn’t looked too deep…
but maybe it helps to use the localPosition instead of position.

I was staring at it for like 5 minutes trying to figure out where you must’ve mixed up local and global coordinates or something. This board really destroys your faith in people after awhile. Glad you found the answer!

2 Likes

Having a mirror may restore it :slight_smile: thanks for the help guys!