x


Acceloration speed limit

Hello,

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?

more ▼

asked Jul 30 '11 at 02:03 PM

Dreeka gravatar image

Dreeka
129 50 62 66

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jul 30 '11 at 02:51 PM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

Something like this. Thanks :)

Jul 30 '11 at 02:54 PM Dreeka
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1948
x137
x99

asked: Jul 30 '11 at 02:03 PM

Seen: 1684 times

Last Updated: Jul 30 '11 at 02:54 PM