Void loop?

Hello, I’m trying to make a car script, and i’m currently working on the acceleration script. The problem is, that I would like the car to slowly/smoothly accelerate, so that it doesn’t go from 100 km/h to 0 instantly, when you release the forward button :slight_smile: Is it possible that I can make the script add maybe 10 to the speed value every second?

Thanks Andreas.

2 Answers

2

Well, can’t give exact code without knowing what data types you’re using, but you’ll want something along the lines of:

void Update()
{
    if(Input.GetKey(KeyCode.Space))
        speed += 10 * Time.deltaTime;
}

to increase the speed 10 units per second while a button is held.

Thanks :-)

Add to your speed using MoveTowards

  if(Input.GetKey("w"))
  {
      speed = Mathf.MoveTowards(speed, maxSpeed, accelerationPerSecond * Time.deltaTime);
  }

Thank you :-) Works perfectly, how can I make the car brake (S), since that should be faster than accelerating? :-)

I kinda figured it out using deccelerationPerSecond and so on :-) Thanks for your help :-)