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));
    	}

Where do you call this method ? See : - https://unity3d.com/learn/tutorials/modules/beginner/scripting/linear-interpolation - http://answers.unity3d.com/questions/998544/having-difficulty-with-lerp.html

This is what you want. http://gamedev.stackexchange.com/questions/102303/2d-c-collecting-item-for-acceleration-unity/102435#102435

1 Answer

1

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;
}