Hold down the key then object will speed up

I make a car modeling.

When I hold down “space”, forward speed of the car will increase.

On the other hand,

When loose “space”, forward speed of the car will decrease until zero.

Just like drive car,

step on the gas, increase the speed of the car,

step off the gas , decrease the speed of the car.

I just can let car move, but think about speed I can’t…

How can I write code? Thank you!

in your key down/key up function…

if (Input.Keydown("SPACE")) {
  accellCounter++;
} else {
  accellCounter--;
}

and somewhere else in your game:

carSpeed = accellCounter;

Now, you’ll need some other stuff in there, like keeping accellCounter between 0 and some max value. Also, maybe a delay timer so you don’t check every single frame.

Google is your friend here. Unity does a very good Car Tutorial here :

Read through it, download it, and try it out.