- Home /
Setting local position after animation is inconsistent
I am using DoTween to tween the position of a stack of images in unity.
for(int i = 0; i < 3; i++)
{
GameObject thisTile = tiles[i];
RectTransform thisPos = thisTile.GetComponent<RectTransform>();
Image thisImage = thisTile.GetComponent<Image>();
if (i==topIndex){
DOTween.To(()=> thisPos.transform.localPosition, y=> thisPos.transform.localPosition = y, new Vector3(0, 5, 0), 1);
} else if (i == secondIndex){
DOTween.To(()=> thisPos.transform.localPosition, y=> thisPos.transform.localPosition = y, new Vector3(0, 0, 0), 1);
} else if (i == thirdIndex){
DOTween.To(()=> thisPos.transform.localPosition, y=> thisPos.transform.localPosition = y, new Vector3(2, -2, 0), 1).OnComplete(handleSwipeUpComplete);
}
}
and then in the onComplete handler, I am attempting to reset the position of the topIndex element:
public void handleSwipeUpComplete(){ if (curIndex < 2){ curIndex++; } else { curIndex = 0; }
GameObject bottomTile = tiles[topIndex];
RectTransform bottomPos = bottomTile.GetComponent<RectTransform>();
Image bottomImage = bottomTile.GetComponent<Image>();
tiles[secondIndex].GetComponent<RectTransform>().SetSiblingIndex(2);
tiles[thirdIndex].GetComponent<RectTransform>().SetSiblingIndex(1);
tiles[topIndex].GetComponent<RectTransform>().SetSiblingIndex(0);
Debug.Log(bottomPos.transform.localPosition);
bottomPos.transform.localPosition = new Vector3(4,-4,0);
}
This works for the first time I do the sequence of animations, but the next time through, even though all of the initial animations happen properly, in the callback, the topIndex
element does not get its position reset. It just stays where it finished in the animation.
Any ideas why, or a better way to handle this?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612170925im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Animation object in wrong place 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Animation callbacks and/or events? 2 Answers