- Home /
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 :-) Is it possible that I can make the script add maybe 10 to the speed value every second?
Thanks Andreas.
Answer by liamcary · Mar 25, 2013 at 10:03 AM
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.
Answer by whydoidoit · Mar 25, 2013 at 10:05 AM
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 :-)