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!