Car acceleration script question

Hey all, right now i’m trying to come up with a script for a car so that it accelerates to top speed.

I’m working off of a basic script and trying to add to it and change it around to make it so that it works, but I’m getting stuck because now it wont work at all (but at least there’s no errors)

any and all help is appreciated.

Here’s my script:

var speed : float = 1;

var acceleration : float = 0.2;

var coasting : float = 0.1;

var braking : float = 0.3;

var turnSpeed : float = 180;

function Update(){

var turning = Input.GetAxis("Horizontal");

var throttle = Input.GetKey("w");

var reverse = Input.GetKey("s");

var braking = Input.GetKey("space");

if (throttle != 0){

	var forwards = (speed + acceleration) * Time.deltaTime;

	transform.Translate(Vector3.forward * forwards);		

}

if (reverse != 0){

	var backwards = (-speed - acceleration) * Time.deltaTime;

	transform.Translate(Vector3.forward * backwards);

}

if (turning != 0 && speed > 1){

	var forwardTurn = turning * turnSpeed * Time.deltaTime * (1);

	transform.rotation.eulerAngles.y += forwardTurn;

}

else if (turning != 0 && speed < -1){

	var backwardTurn = turning * turnSpeed * (-1);

	transform.rotation.eulerAngles.y += backwardTurn;

}

}

In order to make the car accelerate using the translate method, you will have to keep track of how long the user has kept on pressing the forward button, because the velocity of car will be a function of time.

With forces, you dont have to worry about that. If the car has a rigidbody component, then a force in the forward direction will cause acceleration ( force = mass * acceleration, force is specified by you, mass is constant, so you get the acceleration no time involved). Plus you can have drag enabled to some value so that you get the deceleration also.

Using translate is not a really good method, I tried it just a week back for a helicopter, and I got a 140 line code that too had some problems and needed to be expanded. Right now it works on physics and the no of lines is down to 70 or something.

Use rigidbody.AddForce in the FixedUpdate() function.