- Home /
How to animate RectTransform change in position
I have got this function that changes the position of some UI elements when I open their respective menu:
Here's the function that positions the circles based on their position in a list:
void PositionTheCircles()
{
if (gameManager.numberOfOwnedCircles == 1)
{
uIC[0].GetComponent<RectTransform>().position = screenCenter;
}
if (gameManager.numberOfOwnedCircles == 2)
{
uIC[0].GetComponent<RectTransform>().position = new Vector2(Screen.width / 2, Screen.height / 2 - r);
uIC[1].GetComponent<RectTransform>().position = new Vector2(Screen.width / 2, Screen.height / 2 + r);
}
if (gameManager.numberOfOwnedCircles == 3)
{
uIC[0].GetComponent<RectTransform>().position = AngleCalculation(0);
uIC[1].GetComponent<RectTransform>().position = AngleCalculation(120);
uIC[2].GetComponent<RectTransform>().position = AngleCalculation(240);
}
if (gameManager.numberOfOwnedCircles == 4)
{
uIC[0].GetComponent<RectTransform>().position = AngleCalculation(0);
uIC[1].GetComponent<RectTransform>().position = AngleCalculation(90);
uIC[2].GetComponent<RectTransform>().position = AngleCalculation(180);
uIC[3].GetComponent<RectTransform>().position = AngleCalculation(270);
}
}
What I've been trying to do is animate the change in position of the circles. Preferably, I'd like them to move in a circle around the 4-pointed star in the center. But even a straight line would be better than how they just appear in their new positions in one frame.
Answer by BadGrumpyCat · Jul 15, 2017 at 07:48 PM
Use https://docs.unity3d.com/ScriptReference/RectTransform-anchoredPosition.html . This is a position that you can see in transform in editor if rect is anchored to one point (Anchores max x == min x and max y == min y), if anchored are not equal it is still positioned by anchoredPosition, but representation in editor is little different.
Thank you for the reply. Unfortunately, I already knew about that and it hasn't helped me solve the problem.