- Home /
Question by
ihateyourguts · Jul 03, 2017 at 08:32 PM ·
c#movementvelocityhowacceleration
Acceleration problems (don't know how to make it stop moving)
My problem is that my player never deaccelerates to 0 so it is always slightly moving, i don't know how to make it deaccelerate to 0...
float currentXVelocity;
float accelerationRate = 6.0f;
float initialVelocity = 0.0f;
float finalVelocity = 12.0f;
if (Input.GetKey (KeyCode.A)) { // If you press A
currentXVelocity = currentXVelocity - (accelerationRate * Time.deltaTime); // Make your speed accelerate per second
} else {
if (currentXVelocity < initialVelocity) { // If you are still moving left, slow down
currentXVelocity = currentXVelocity + (deaccelerationRate * Time.deltaTime);
}
}
if (Input.GetKey (KeyCode.D)) {
currentXVelocity = currentXVelocity + (accelerationRate * Time.deltaTime);
}
else {
if (currentXVelocity > initialVelocity) {
currentXVelocity = currentXVelocity - (deaccelerationRate * Time.deltaTime);
}
}
currentXVelocity = Mathf.Clamp (currentXVelocity, -finalVelocity, finalVelocity); // Clamp the currentXVeclotiy to -finalVelocity and finalVelocity
velocity.x = currentXVelocity;`enter code here`
Comment
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Trouble with diagonal acceleration in Quake style movement 0 Answers
Player slows down when jumping/velocity changes 1 Answer
Carry forward velocity against mouse movements 1 Answer
How to make bullets moving proportionally faster than player? 4 Answers