(Noob) My Character isn't Decelerating! Help?

4.2.1

const MAX_SPEED = 300.0
const ACCELERATION = 15.0
const DECELERATION = -100.0

var direction = Input.get_axis(“ui_left”, “ui_right”)
if not direction == 0:
velocity.x += ACCELERATION * direction
if (abs(velocity.x) > MAX_SPEED):
velocity.x = MAX_SPEED * direction
else:
velocity.x += DECELERATION * direction
if (abs(velocity.x) < 0):
velocity.x = 0

move_and_slide()

My character isn’t Decelerating!
I’ve tried a lot of techniques. I made DECELERATION positive, I got rid of the second to fourth last lines, making sure the line was even being processed; every trick in the book. Acceleration works as well as it can, but deceleration doesn’t. Help?

A guess at the indentation:

const MAX_SPEED = 300.0
const ACCELERATION = 15.0
const DECELERATION = -100.0

# is all of this inside the _process function or something like that?
var direction = Input.get_axis(“ui_left”, “ui_right”)
if not direction == 0:
    velocity.x += ACCELERATION * direction
    if (abs(velocity.x) > MAX_SPEED):
        velocity.x = MAX_SPEED * direction
else:
    velocity.x += DECELERATION * direction
    if (abs(velocity.x) < 0):
        velocity.x = 0

move_and_slide()

If my guess about the indentation here is correct, then your problem is as follows: In the else branch of the outer if-statement, you’re multiplying DECELERATION with direction, but because you’re in that else-branch, we know that direction must be 0. Anything multiplied by 0 is 0, so no deceleration gets added.

To fix it, use sign(velocity.x) to determine which way the object is currently moving in the case where direction is 0. That is, look at which way the object is actually moving, rather than which way the player is asking it to move (since the player isn’t asking it to move anywhere).

2 Likes

I have another question!
With the same code, the
if (abs(velocity.x) < 0):
velocity.x = 0
doesn’t work, because the absolute value of a number is never less than zero.
How else can I make sure that the velocity never changes the sign of velocity.x? I’ve already tried
if sign(velocity.x) < 0:
if (velocity.x - DECELERATION) < 0:
velocity.x = 0
else:
if (velocity.x + DECELERATION) > 0:
velocity.x = 0
I’ve been scratching my head. Any thoughts?

Make sure to use code blocks by pressing the </> button on a new line and pasting code like so

```
type or paste code here
```

move_toward is pretty good at this stuff, could compare signs for direction. Might need a special zero clause for decelration

var direction := Input.get_axis(“ui_left”, “ui_right”)

var is_decel := sign(velocity.x) != sign(direction) or direction == 0
var accel := DECELRATION if is_decel else ACCELERATION
velocity.x = move_toward(velocity.x, direction * MAX_SPEED, accel)
1 Like

Try calculating the new velocity in a separate variable, then checking if the sign is different from the old velocity:

var new_velocity = velocity.x + DECELERATION * sign(velocity.x)
if (sign(velocity.x) != sign(new_velocity)):
    new_velocity = 0
velocity.x = new_velocity
1 Like