Car Driving problem!

OK, so, I’ve tried to make a script that is attached to a rigid body(the interior of a vehicle), and just make it so, so you can drive, guess what, you may say whatever you want, but if you test it, you’ll see it works out pretty well(I am saying this because the script is pretty short).The problem is that I want you to tell me how to script the brakes in a better way.And also, how to make the vehicle move forward according to the CarSpeed, I mean, in game everything works perfect, JUST THAT when I press W, the car starts slowly(as it’s supossed to do), but then just simply flies, and if I collide on a fence or something, well, ACCIDENT, boom :smile:.Here’s the code:

using UnityEngine;
using System.Collections;

public class VehicleScript : MonoBehaviour 
{
	public float CarSpeed;
	public float MaxCarSpeed;
	public GameObject Car;
	public Vector3 CurPos;
	public Vector3 LastPos;
	void Start()
	{
		Car=GameObject.Find ("/Interior");
		CarSpeed=0.0f;
		MaxCarSpeed=1.5f;
	}
	void Update() 
	{
		rigidbody.drag=0.0f;
		CurPos=rigidbody.position;
	    if(Input.GetKey(KeyCode.W))
		{
			if(CarSpeed!=MaxCarSpeed)
			{
				CarSpeed+=0.1f;
			    rigidbody.AddForce(transform.forward*CarSpeed,ForceMode.VelocityChange);
			}
		}
		if(Input.GetKey(KeyCode.A))
            rigidbody.transform.Rotate(Vector3.down * CarSpeed);
		if(Input.GetKey(KeyCode.D))
			rigidbody.transform.Rotate(Vector3.up * CarSpeed);
		if(Input.GetKey(KeyCode.S))
		{
			if(CurPos!=LastPos)
                        {
				rigidbody.drag=2.0f;
				CarSpeed-=0.1f;
                        }
		}
		LastPos=CurPos;
	}
}

The car just simply, BOOM, takes off like a rocket, even if the speed is small.

Your code doesn’t account for framerate. Try using transform.forward*CarSpeed__*Time.deltaTIme__. (you’ll have to adjust your CarSpeed multiplier to work in Seconds) Also you should probably be doing these physics calculations in FixedUpdate. I don’t think your “S” code will work the way you want reliably because of the inaccuracy of the physics system. You probably will not be able to get away with comparing positions to decide when you have stopped. Use (rigidbody.velocity < .001f) instead. Simpler too.

Have you looked at the Unity Car tutorial?
http://unity3d.com/support/resources/tutorials/car-tutorial

Really good start for driving in Unity.