# Acceloration speed limit

 0 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 129 ● 50 ● 62 ● 66 SisterKy 2.5k ● 33 ● 42 ● 60 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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 42.5k ● 16 ● 43 ● 202 Something like this. Thanks :) Jul 30 '11 at 02:54 PM Dreeka add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x1948
x137
x99

asked: Jul 30 '11 at 02:03 PM

Seen: 1684 times

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