Question by
Claff · May 05, 2016 at 03:27 PM ·
positioningrecttransformlerping
Why Lerping Rect Transform position, results in strange behaviour?
Hi everyone. So my problem is really simple. I want to create a Text animation, via position Lerping. The Math behind the Lerping function works, but when I test my game, something strange with the position happens.
This is what I have before playing the game.
This is what happens when I play it.
I think my text uses normal positioning, instead of Canvas related positioning but I can't figure out how to distinguish the two.
Here is the script by the way...
public class Animation_Manager : MonoBehaviour {
[SerializeField]
private GameObject[] Elements;
[SerializeField]
private float movementLerpTime;
[SerializeField]
private float fadeLerpTime;
private float currentMovementLerpTime = 0f;
private float currentFadeLerpTime = 0f;
private Vector3[] startPositions;
private Vector3[] endPositions;
void Awake () {
/*for (int i = 0; i < Elements.Length; i++)
{
GameObject current_element = Instantiate(Elements[i]);
current_element.transform.SetParent(this.transform, false);
}*/
startPositions = new Vector3[Elements.Length];
endPositions = new Vector3[Elements.Length];
for (int i = 0; i < Elements.Length; i++)
{
startPositions[i] = Elements[i].GetComponent<RectTransform>().position;
Debug.Log(Elements[i].gameObject.name + " - " + startPositions[i]);
endPositions[i] = startPositions[i] + Vector3.right * 10f;
Debug.Log(Elements[i].gameObject.name + " - " + endPositions[i]);
}
}
// Update is called once per frame
void Update () {
if (currentMovementLerpTime != movementLerpTime)
{
for (int i = 0; i < Elements.Length; i++)
{
currentMovementLerpTime += Time.deltaTime;
if (currentMovementLerpTime > movementLerpTime)
{
currentMovementLerpTime = movementLerpTime;
}
float t = currentMovementLerpTime / movementLerpTime;
t = Mathf.Sin(t * Mathf.PI * 0.5f);
Elements[i].transform.position = Vector3.Lerp(startPositions[i], endPositions[i], t);
}
}
}
}
I hope somebody could help me or at least get me some documentation about this subject. Thank You very much in advance ! :D
snapshot-unity-before.png
(106.6 kB)
snapshot-unity-after.png
(102.3 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612080224im_/https://answers.unity.com/themes/thub/images/avi.jpg)