Acceloration speed limit


I am limiting the maximum speed of my marble with this way:

curSpeed = Vector3(rigidbody.velocity.x, 0, rigidbody.velocity.z).magnitude;

if (curSpeed > maxSpeed)
		rigidbody.velocity = Vector3(rigidbody.velocity.x, 0, rigidbody.velocity.z).normalized * maxSpeed + rigidbody.velocity.y * Vector3.up;

This way the marble can’t reach faster speed no matter to what. However, I would only like to limit the acceloration speed, so the marble could go faster when rolling down a hill, or when a stronger force is applied to it.

I have tried to put a limitation to the code, which controlls the acceloration, however than the marble is much slower.
The limitation:

 if ((onGround) && (curSpeed > maxSpeed))


How should I make a limit to the acceloration only?

I don’t know exactly what you’re trying to do, but I’ll assume that you want the marble to run at a constant horizontal velocity, but still being affected by gravity and external forces. If this is the case, you must use a kind of motor to apply force only when needed. Maybe something like this works for you:

var maxSpeed: float = 10; // speed limit
var motorForce: float = 15; // acceleration force, kinda the engine HP
// this is the direction the motor is pushing the marble to
// change this to control the direction
private var forceDir: Vector3 = Vector3.forward;

function FixedUpdate(){
  var curSpeed = rigidbody.velocity;
  curSpeed.y = 0; // ignore vertical velocity
  if (curSpeed.magnitude < maxSpeed){  // need to step on the gas?
    rigidbody.AddForce(motorForce * forceDir.normalized);

This will make the marble accelerate to max speed, but other forces or collisions will still affect its trajectory and speed. The force will be applied at the direction defined by forceDir: change it based on Input.GetAxis if you want to control its direction.