decreasing speed with deltaTime error

So, I want to make a very simple driving script.
I finally got it to working almost the way I want to, exept one thing:

When W is not pressed, the current speed should be decreased by a fixed value per second, so the car doesn’t stop instantly.

Obviously using Time.deltaTime is the way to go, but for this part of the script it doesn’t seem to work - the value is subtracted each frame instead of each second.

I’m checking my script for hours now, tinkering with values and brackets, but i can’t find my error.

Here’s my code:

	var acceleration : float = 0.0;
	var maxSpeed : float = 10.0;
	var speed : float = 0.0;
	var jumpSpeed : float = 8.0;
	var gravity : float = 20.0;
	var rotationSpeed : float = 5;
	var gear : int = 0;
	var gearcheck : int = 0;

function Start (){
	 acceleration = 0.0;
	 speed = 0.0;
	 maxSpeed  = 20.0;
	 rotationSpeed = 3;
	 }
	

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

// Acceleration to 0 on gear change

//set comparison gear
gearcheck = gear;

//set gear ( = direction)
if (Input.GetAxis("Vertical") > 0){
	gear = 1;
	}	
else if(Input.GetAxis("Vertical") < 0){
	gear = -1;
	}
	
// compare gears/directions ( set acceleration to 0 on change)	
if (gear != gearcheck){
acceleration = 0;
}
	//Driving
	if (Input.GetAxis("Vertical")){
	speed = (Input.GetAxis("Vertical"))* acceleration;
	transform.Translate(Vector3(0, speed, 0)* Time.deltaTime);
		
		// accelerating (while speed < maxspeed)
		if (acceleration < maxSpeed){
		acceleration = acceleration +7 * Time.deltaTime; 
			}
	}
	
	// decrease of speed (when no input is given) 
    // THIS IS WHERE I THINK MY PROBLEM IS LOCATED		
	else if (speed > 1.6 || speed < -1.6){
	speed = speed -(-gear * 2) * Time.deltaTime ; 
	transform.Translate(Vector3(0, speed , 0)* Time.deltaTime);
	}
	else {
	acceleration = 0;
	speed = 0;
	}
	
	// steering
	if (speed != 0){
	transform.Rotate (Vector3 (0, 0, Input.GetAxis("Horizontal") * rotationSpeed * speed) * Time.deltaTime );
	} 
	
	
	print("Accelleration: " + acceleration + " / Speed: " + speed + " / Gear: " + gear); 
}	

Thanks in advance

I have to agree with Tanoshimi. Your physics us rather poorly done. Acceleration is a rate of change in speed, where speed is a change of location. So, to get the desired functionality, you will have to rewrite it. Just keep these in mind:

if(Input != 0)[ speed += Input * accelerationRate; ]

if(Input == 0)[ speed -= friction * Time.deltaTime; ]

Input.GetAxis returns as a float between -1 and 1 so you don’t need to put the gear thing.

speed = speed -(-gear * 2) * Time.deltaTime ;
transform.Translate(Vector3(0, speed , 0)* Time.deltaTime);

Your speed is a function of deltaTime. In the next line you use speed to move and then also make that a function of deltaTime.

Is this intentional?

As your speed varies by time, try leaving time out of the translate in that section.

You’re getting horribly muddled between “speed” and “acceleration” in several places in your script. “Speed” is the rate of change of distance. “Acceleration” is the rate of change of velocity.

But, in this line, you’re setting the magnitude of “speed” equal to “acceleration” (as you’re talking about keyboard input, Input.GetAxis will always either return -1, 0, or 1):

speed = (Input.GetAxis("Vertical"))*acceleration;

Likewise, in this line, you’re comparing acceleration to max speed, which doesn’t really make any sense:

 if (acceleration < maxSpeed){

And then you’re incrementing acceleration by 7 every second (where did the number 7 come from?)

acceleration = acceleration +7 * Time.deltaTime;

Acceleration (or, even better, force) should be what is set based on the keyboard input, not speed. And acceleration should be zero when there is no keyboard input, but what you’ve written is for acceleration to remain constant until either speed drops below 1.6, or you reverse direction, at which point it suddenly drops to zero. Basically, your physics is all over the place!

I think you should rewrite this with careful thought about what is going on. It might help to refer to some basic mechanics texts, such as Newton's second law - Newton's laws - OCR Gateway - GCSE Combined Science Revision - OCR Gateway - BBC Bitesize