Biped (Dinosaur) Rigidbody Character Controller - Acceleration

Hi folks,

I am wondering how to achieve realistic acceleration and deceleration with a rigid body controller, as large animals don’t tend to instantly reach 20 kmh… :).

Currently I am using an impulse force but I can’t seem to have it gradually accelerates.

I am just looking for some code exampls (c#).

Thanks for your time.

There are multiple ways to do this. Some quick examples would be:

Using addForce to slowly gain speed over time. Remember to set the drag attribute of your rigidbody to limit the max speed:

public class AccelerateExample : MonoBehaviour {
    public float accelerationSpeed = 1.0f;
    private Rigidbody rigidbody;

    private void Start () {
        rigidbody = GetComponent<Rigidbody>();
    }

    private void FixedUpdate()
    {
        // Add speed in the direction the object is facing
        rigidbody.AddForce(transform.forward * accelerationSpeed);
    }
}

Another way, which gives you a little more control over the top speed and the time to reach it, is setting the velocity yourself using linear interpolation:

public class AnotherAccelerateExample : MonoBehaviour
{
    public float maxSpeed = 5;
    public float secondsToReachMaxSpeed = 10;

    private Rigidbody rigidbody;
    private float startTime;

    private void Start()
    {
        rigidbody = GetComponent<Rigidbody>();
        startTime = Time.time;
    }

    private void Update()
    {
        float timePast = Time.time - startTime;
        float currentSpeed = Mathf.Lerp(0, maxSpeed, timePast / secondsToReachMaxSpeed);
        rigidbody.velocity = transform.forward * currentSpeed;
    }
}