Hoping for a explaination about lerp over time...

ok, I have looked through about 30 examples, spent the night going back and forth over the documentation and searching the forums and I feel safe to say the time has come for me to simply as the question.

How the Mathf.Lerp function actually works is just beyond me at the moment. What I am trying to accomplish is relatively simple. I have a current speed, a top speed and an acceleration rate. Lets say my object is not moving, so its current speed is 0, its top speed could be, lets say 60. The acceleration rate is the amount of time in second I would like it to take to go from 0 to topspeed.

The only way that comes to mind is by having a variable keep track of the time that I start the acceleration, and using that to figure out what time it should reach top speed, like this:

	function Accelerate(currSpeed:float, startTime:float){
		var targetTime = startTime + accelRate;
		var remainingTime = targetTime - Time.time;
		var accelPrecent = (accelRate - remainingTime) / accelRate;
		
		var newSpeed:float = Mathf.Lerp(currSpeed, topSpeed, accelPrecent);
		
		return newSpeed;
	}

but that just seems like its overly complicated. And not only that but releasing the accelerate button would force the accelerate timer to then reset and take the full accelerate time to then go from current speed to topspeed, rather than from 0 to topspeed. And frankly doesn’t work right anyway. It actually reaches its top speed in about half as much time as the accel rate should let it.

Now I know I should be able to use variables such as Time.deltaTime to my advantage here, but I just cant grasp how I could do so, and when I have gotten lerps to appear to work in the past using Time.deltaTime in calculating the t parameter of lerp like this, it never seems to actually reach the destination value.

I would greatly appreciate if someone could explain this chronological nightmare to me so I can figure out what I’m doing wrong and be able to actually use this again in the future.

Thanks,
Alima

This might help some.

–Eric

There’s a few ways to do this, but I wouldn’t use Lerp for this one. I would use Time.deltaTime

var speed = 0.0;
var accelRate = 10; // add 10 per second, or 0-60 in 6 seconds for this example
var maxSpeed = 60.0;

function Accelerate(){
   if (speed < maxSpeed){
       speed = speed + (Time.deltaTime * accelRate);  
   }
}

You could just divide the past time by the total time

float startTime;
float totalDuration = 5.5f; // tween over five and a half seconds

void Start(){
    startTime = Time.realtimeSinceStartup;
}

void Update(){
     interpolatedValue = Mathf.Lerp(startingValue, endingValue, (Time.realtimeSinceStartup - startTime) / totalDuration);
}