# Decremint at a consistant rate

Hi all,

In the game I’m working on, there is a button the player can press to slide across the screen. When the button is pressed, the player gradually slows down until their speed reaches 0. Then they can move again.

The problem is, the rate of deceleration doesn’t seem to be consistent. Sometimes it will take a two seconds to slow down, other times it will take just one or even less. Is there a way to ensure consistency? This is the code I’m using to slow them down:

``````float slidingDecrement = 2f;

if (slidingSpeed >= 0)
slidingSpeed -= slidingDecrement * Time.deltaTime;
``````

Thanks!

EDIT

Here is some further code (stuff I’m sure is irrelevant has been removed):

``````	void Update () {
if (sprinting && !sliding)
Slide();

if (sliding)
Slide();

if (!sliding)
SprintCheck();

if (stunTime <= 0 && !sliding){
Movement();
Shoot();
}

}

void Slide(){

if (Input.GetKeyUp(KeyCode.E) && !sliding && stamina >= 10)
{
sliding = true;
stamina -= 10;
}

if (sliding)
{
Vector3 newPosition = transform.position;

if (direction == "UpLeft"){
newPosition.x -= slidingSpeed * Time.deltaTime;
newPosition.y += slidingSpeed * Time.deltaTime;
}else if (direction == "DownLeft"){
newPosition.x -= slidingSpeed * Time.deltaTime;
newPosition.y -= slidingSpeed * Time.deltaTime;
}else if (direction == "UpRight"){
newPosition.x += slidingSpeed * Time.deltaTime;
newPosition.y += slidingSpeed * Time.deltaTime;
}else if (direction == "DownRight"){
newPosition.x += slidingSpeed * Time.deltaTime;
newPosition.y -= slidingSpeed * Time.deltaTime;
}else if (direction == "Right"){
newPosition.x += slidingSpeed * Time.deltaTime;
}else if (direction == "Left"){
newPosition.x -= slidingSpeed * Time.deltaTime;
}else if (direction == "Up"){
newPosition.y += slidingSpeed * Time.deltaTime;
}else if (direction == "Down"){
newPosition.y -= slidingSpeed * Time.deltaTime;
}

transform.position = newPosition;
}

if (slidingSpeed >= 0)
slidingSpeed -= SLIDE_DECRIMENT * Time.deltaTime;
else if (slidingSpeed < 0 && sliding)
{
slidingSpeed = MAX_SLIDE_SPEED;
sliding = false;
sprinting = false;
}
``````

you could use a rigidbody and -= velocity rather then position.

stamina -= 10; — stamina -= 10 * Time.deltaTime;

Any constant number changing without deltatime is subject to change based on your FPS. So if you’re at 50 FPS one time, then at 100 FPS the next time, stamina will drain twice as quick as it’s now running 100 times a second instead of 50.

Also you can use Lerp for a really nice smooth slide.

``````//Set the slide distance once
slideSpeed = new Vector3(10, 0, 10);

//In update, the slide distance moves to 0 over 2 seconds
slideSpeed = Vector3.Lerp(slideSpeed, Vector3.zero, Time.deltaTime*0.5f);
//Add the slide distance to the position
transform.position = transform.position + slideSpeed;
``````