How to Use Lerp for Velocity in Unity?

Hi everyone,

I am a beginner in Unity, coming from a web development background, and I have basic physics knowledge.

I am experimenting with a bike model and trying to learn how to use Lerp to update its velocity. However, I’m encountering an issue: when I go forward and then turn right, and then stop pressing any keys, the bike slides instead of stopping smoothly. It even seems to slide in the wrong direction.

Here is my current code for updating the bike’s velocity and rotation:

void Acceleration()
{
    sphereRB.velocity = Vector3.Lerp(sphereRB.velocity, moveInput * maxSpeed * transform.forward, Time.fixedDeltaTime * acceleration);
}

void Steer()
{
    transform.Rotate(0, steerInput * steerStrength * Time.fixedDeltaTime, 0, Space.World);
}

Here is the video demonstration

I have a few questions:

  1. How can I properly use Lerp for velocity to avoid the sliding issue?
  2. Do I need to use InverseTransformDirection to obtain the velocity in local coordinates and multiply them? This question specifically applies to the rotation.

I would appreciate any advice or suggestions. Thank you!

Have you tried a wheel collider?. They have settings for controlling lateral friction.

If you don’t want to use a wheel collider then you’ll need to control the friction yourself and I don’t think Lerp is the answer. You also shouldn’t be setting the rigidbody velocity directly as this physics engine also needs to modify the velocity for gravity and collision deflection.

Instead you should use AddForce to move your bike around. And you could use Vector3.Dot to add some lateral friction:

rb.AddForce(-rb.velocity*Mathf.Abs(Vector3.Dot(transform.right,rb.velocity.normalized))*5); // lateral friction