- Home /
Question by
Connor2164 · Aug 16, 2015 at 03:54 PM ·
lerpcarmathf.lerpaccelerate
Using Lerp to accelerate objects
i am trying to accelerate a car over time. when the a key is pressed, it passes a targetVelocity of 5.0 i want that car to accelerate to that value slowly over time using math.lerp, I just can't seem to get it to work. This code causes the car to travel at a constant velocity.
public void MoveCar(float targetVelocity){
transform.position += transform.right * (Mathf.Lerp(0.0f, targetVelocity, Time.deltaTime * 3.0f));
}
Comment
Answer by nomadic · Aug 16, 2015 at 05:30 PM
You are not actually interpolating anything...you need to cache the final velocity. The statement (Mathf.Lerp(0.0f, targetVelocity, Time.deltaTime * 3.0f)) always evaluates to the roughly the same velocity.
float currentVelocity;
public void MoveCar(float targetVelocity) {
currentVelocity = (Mathf.Lerp(currentVelocity, targetVelocity, Time.deltaTime * 3.0f))
transform.position += transform.right * currentVelocity * Time.deltaTime;
}