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.
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: