The question is answered, right answer was accepted
How to increase and decrease speed gradually?
I have a Rigidbody attached to car, which is controlled by the player. What I need is a code to make the car gain speed gradually when "UpArrow" or "W" is pressed and gradually decrease speed when the key is released. I've searched online but I have found only code that is not compatible with the latest version of unity. Thanks in advance!
Answer by AurimasBlazulionis · Sep 12, 2016 at 12:58 PM
Uhm... it is actually vey simple. Have a float speed variable. Also have max speed variable. Now, inn update if (Input.GetAxisRaw("Horizontal") > 0f)
, then add to speed some increment value. Else, remove from speed that increment value. So to add speed you would write speed += increment * Time.deltaTime
.
Then to make sure your speed does not exceed limits, after those checks call speed = Mathf.Clamp (speed, 0f, maxSpeed);
.
After that it is up to you to use that variable anyhow you like.
Follow this Question
Related Questions
Speed pad/booster 3D 1 Answer
my rigidBody.velocity return values that are false 1 Answer
Space Shooter Bolt Speed Forward Issue 2 Answers
Getting the rigibody horizontal and vertical speed before added to their position? 0 Answers
Getting two Objects to the same direction with different speed 0 Answers