# Script for car movement

Hey all,

I’m just starting back up with working with unity and scripting with JavaScript again and it seems like I’ve forgotten everything I knew about it before.

I was wondering how I would make a car (just a rectangle box at the moment) move forwards and backwards with the front and back arrow keys, and with the left and right arrow keys turn instead of move in that direction.

Right now I would just like to know the basics of how to do this, but eventually I would like to know how to tweak things so that it takes longer to accelerate/stop, turns wider or sharper, etc etc.

Thanks for any input

basic car controls 101, comin' up!

``````//inspector variables

var speed:float=5; //speed of the car, tweek as needed based on your scale
var turnSpeed:float=180; //turn speed

function Update()
{
//grab the input axes
var steer=Input.GetAxis("Horizontal");
var gas=Input.GetAxis("Vertical");

//if they're hittin' the gas...
if (gas!=0)
{
//take the throttle level (with keyboard, generally +1 if up, -1 if down)
//  and multiply by speed and the timestep to get the distance moved this frame
var moveDist=gas*speed*Time.deltaTime;

//now the turn amount, similar drill, just turnSpeed instead of speed
//   we multiply in gas as well, which properly reverses the steering when going
//   backwards, and scales the turn amount with the speed
var turnAngle=steer * turnSpeed * Time.deltaTime * gas;

//now apply 'em, starting with the turn
transform.rotation.eulerAngles.y+=turnAngle;

//and now move forward by moveVect
transform.Translate(Vector3.forward*moveDist);
}

}
``````

Just tested that in an empty scene controlling a cube (pos: 0,0,0; rotation 0,0,0; scale: .5, .5, 1), set the camera above the scene looking down (pos: 0,30,0; rotation: 90,0,0), threw in a directional light just to give it some shading, works like a charm.

Of course, there are a lot of things "wrong" with this code you'll need to change or fix later, but this should help you get started and might give you an idea how to handle driving controls in general!