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.
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.
Add to your speed using MoveTowards
if(Input.GetKey("w"))
{
speed = Mathf.MoveTowards(speed, maxSpeed, accelerationPerSecond * Time.deltaTime);
}
Thanks :-)
– AndreasX12