- Home /
How to lerp over time help?
Hi everyone. This is probably a simple question, but I have this Lerp script - if (moving == true){ transform.position.x = Mathf.Lerp(-8.5, 2.3, 1);} } How can I make the lerp move from one point to another over a time period of 2 seconds instead of instantly?
Comment
Best Answer
Answer by dmg0600 · Sep 20, 2014 at 05:33 PM
Change the 1 in the Lerp for the time. Note that it has to be between 0 and 1.
float currentTime = 0f;
float timeToMove = 2f;
void Update() {
if (moving == true)
{
if (currentTime <= timeToMove)
{
currentTime += Time.deltaTime;
transform.position.x = Mathf.Lerp(-8.5f, 2.3f, currentTime / timeToMove);
}
else
{
transform.position.x = 2.3f;
currentTime = 0f;
moving = false;
}
}
}