Character acceleration.

Hi, How can I make acceleration to my chracter? my speed is 20, but no acceleration allways when I press UP speed is 20. How can I put acceleration to my chracter so when I press up it starts to speed up and when I don’t press UP anymore it starts to slow down.
Like a car.
My character move script:

var speed : float = 3.0;
var rotateSpeed : float = 3.0;
var bullitPrefab:Transform;

function Update () {
var controller : CharacterController = GetComponent(CharacterController);

// Rotate around y - axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);

// Move forward / backward
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed : float = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if(Input.GetButtonDown("Jump"))
{
var bullit = Instantiate(bullitPrefab,
                                GameObject.Find("spawnPoint1").transform.position,
								Quaternion.identity);
								bullit.rigidbody.AddForce(transform.forward * 1000);

}

}
@script RequireComponent(CharacterController)

A simple method is to make a variable that stores the current speed, for example, you’re using:
curSpeed

Make this a float and create it at the start of your script:

var curSpeed : float = 0.0; 

You will also need a few more variabes:

var acc : float = 1.0; // This is how fast it will accelerate.
var maxSpeed : float = 20.0; // This is the players maximum speed.

Then, when you come to detect the input from the keyboard you add to or subtract from this variable.

curSpeed += acc*Input.GetAxis ("Vertical");

After this you need to ‘Clamp’ the curSpeed to the maximum speed, this stops your player from speeding up too much:

curSpeed = Mathf.Clamp(curSpeed,-maxSpeed,maxSpeed); // Clamps curSpeed

And finally you need to decelerate the player when no buttons are pressed:

if(Input.GetAxis("Vertical") == 0) 
{
  if(curSpeed > acc) curSpeed -= acc; 
  if(curSpeed < -acc) curSpeed += acc;
}

I haven’t tested this, but the theory is there. I know there’s certainly a better way to apply deceleration, but I can’t remember it right now.

You can check the “run” key (Left Shift in this script) and set the target speed to walk or run, then Lerp to the target speed:

var walk: float = 3.0; // walk speed
var run: float = 8.0;  // run speed
private var speed: float = 0;
var rotateSpeed : float = 3.0;
var bullitPrefab:Transform;

function Update () {
  var controller : CharacterController = GetComponent(CharacterController);
  // Rotate around y - axis
  transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
  // Move forward / backward
  var forward : Vector3 = transform.forward;
  var newSpeed = walk; // start newSpeed as walk
  if (Input.GetKey("left shift")) newSpeed = run; // change to run if left shift pressed
  speed = Mathf.Lerp(speed, newSpeed, Time.deltaTime); // smoothly change speed to newSpeed

  var curSpeed : float = speed * Input.GetAxis ("Vertical");
  controller.SimpleMove(forward * curSpeed);
  if(Input.GetButtonDown("Jump")){
    var bullit = Instantiate(bullitPrefab,
                             GameObject.Find("spawnPoint1").transform.position,
                             Quaternion.identity);
    bullit.rigidbody.AddForce(transform.forward * 1000);
  }
}

@script RequireComponent(CharacterController)