Ideas:
The Lerp function takes Vector3 as an example:
Vector3.Lerp(Pos1,Pos2,LerpStep)
Pos1=starting position (usually transform.positionon, that is, the real-time position of each frame object; for UI objects, it is RectTransform, as follows)
Pos2=end position
LerpStep=The proportion completed by calling the Lerp function each time
for example:
I put the object at (0,0,0)
Vector3.Lerp(transform.position,new Vector3(10,0,0),0.1f)
When this function is called for the first time, the X coordinate of the object is: 0+(10-0)*0.1=1
The first position becomes (1,0,0)
Second time: (1+(10-1)*0.1,0,0)=(1.9,0,0)
The third time: (1.9+(10-1.9)*0.1,0,0)=(2.71,0,0)
Therefore, to achieve a uniform speed, the step size needs to be changed according to the process
Still take the above example as an example to achieve uniform speed:
t= 10/(10-transform.position.x)*0.1;//t=total distance/(total distance-current distance)*step length
Vector3.Lerp(transform.position,new Vector3(10,0,0),t)
When this function is called for the first time, t=1*0.1, the X coordinate of the object is: 0+(10-0)*0.1=1
The first position becomes (1,0,0)
For the second time, t=10/(10-1)*0.1=(10/9)*0.1, the X coordinate of the object is: 1+(10-1)*(10/9)*0.1=2
The second position becomes (2,0,0)
The third time: t=10/(10-2)*0.1=(10/8)*0.1, the X coordinate of the object is: 2+(10-2)*(10/8)*0.1=3
And so on (personal idea, not necessarily correct)
The following is my code for implementing UI movement:
public class ReadBook : MonoBehaviour
{ public RectTransform Page1; public TMPro.TextMeshProUGUI Page1Text; public RectTransform Page2; public TMPro.TextMeshProUGUI Page2Text;
private bool isLerp;
float t;
float OriginalLength;
float currentLength;
public float LerpStep=0.1f;//Complete 10% each time, multiplied by Time.deltaTime, that is, the total completion time=1/0.1=10 seconds
public RectTransform LastPage;
// Start is called before the first frame update
void Start()
{
OriginalLength = (Page2.localPosition - LastPage.localPosition).magnitude;
Debug.Log("原长为:" + OriginalLength);
}
// Update is called once per frame
void Update()
{
if (isLerp)
{
t = OriginalLength / currentLength * LerpStep*Time.deltaTime;
Page2.localPosition = Vector3.Lerp(Page2.localPosition, LastPage.localPosition, t);
if (Page2.localPosition== LastPage.localPosition)
{
Debug.Log("Lerp position has ended");
isLerp = false;
}
}
}
}