help with breaking the car acceleration slowly

i’m just wondering how can i break slowly once i pressed “j”? :smile:

here is my code…

var maxSpeed:float=100;
var reverseSpeed:float=30;
var acceleration:float=30;
var keyforward=“w”;
var keyleft=“a”;
var keyright=“d”;
var keyreverse=“s”;
var rotateSpeed= 1.0;
private var grounded:boolean;

private var curSpeed:float=0;

function Update () {
var controller : CharacterController = GetComponent(CharacterController);
var forward = transform.TransformDirection(Vector3.forward);
var up = transform.TransformDirection(Vector3.up);
if(Input.GetAxis(“Vertical”) > .1)
{

if(Input.GetKey(keyforward))
{
animation.CrossFade(“run”);
animation[“run”].wrapMode= WrapMode.Loop;
}

if(Input.GetKeyUp (keyforward))
{
animation.CrossFade (“idle”);
animation[“idle”].wrapMode = WrapMode.Once;
}

//THIS IS THE CODE THAT IM TRYING TO FIX FOR PRESSING “J”

if(Input.GetKey(“j”))
{
curSpeed=0;
}
//end

if(curSpeed < maxSpeed)
{
curSpeed += acceleration * Input.GetAxis (“Vertical”) * Time.deltaTime;
}else{
curSpeed = maxSpeed;
}
}
else if(Input.GetAxis(“Vertical”) < -.1)
{
curSpeed += acceleration * Input.GetAxis (“Vertical”) * Time.deltaTime;
if(curSpeed <= -reverseSpeed)
{
curSpeed = -reverseSpeed;
}

//reverse movement
if (Input.GetKey(keyreverse))
{
animation.CrossFade(“reverse”);
animation[“reverse”].wrapMode = WrapMode.Loop;
}

if(Input.GetKeyUp(keyreverse))
{
animation.CrossFade(“idle”);
animation[“idle”].wrapMode = WrapMode.Once;
}
}

var flags = controller.SimpleMove(forward*curSpeed);
if(flags CollisionFlags.CollidedBelow)
{
grounded = true;
}else{
grounded = false;
}

//left movement
transform.Rotate(0, Input.GetAxis (“Horizontal”) * rotateSpeed ,0);

if (Input.GetKey(keyleft) )
{
animation.CrossFade (“left”);
animation[“left”].wrapMode = WrapMode.Loop;
}

if(Input.GetKeyUp (keyleft))
{
animation.CrossFade (“idle”);
animation[“idle”].wrapMode = WrapMode.Once;
}

//right movement
if (Input.GetKey(keyright) )
{
animation.CrossFade (“right”);
animation[“right”].wrapMode = WrapMode.Loop;
}

if(Input.GetKeyUp (keyright))
{
animation.CrossFade (“idle”);
animation[“idle”].wrapMode = WrapMode.Once;
}
}

Instead of setting the speed directly to zero when J is pressed, you could just subtract a small amount from its value:-

speed = Math.Max(speed - smallAmount * Time.deltaTime, 0);

The Mathf.Max call ensures that the speed never goes below zero (ie, the car starts going backwards rather than braking). Multiplying the smallAmount value by Time.deltaTime takes the framerate into account, so the car doesn’t brake more sharply on faster computers.