Godot Version
4.4.1-stable
Question
Hey all!
Implementing acceleration into a CharacterBody3D
was fairly straightforward, using velocity.move_toward
to accelerate towards a maximum speed, but how should I implement deceleration? In my case, I wanted my deceleration to be quite a bit more than my acceleration, for snappy movement, but without an instant.
My first thought was to simply use deceleration when the player is not holding an input, like this:
# dir is a Vector2 for input direction
# speed_sprint is my max speed in this context
if dir != Vector2.ZERO:
velocity = velocity.move_toward(move_dir * speed_sprint, acceleration * delta)
else:
velocity = velocity.move_toward(Vector3.ZERO, deceleration * delta)
Here, deceleration works properly when the player isn’t holding a direction, but what if they ARE holding a direction, it’s just a different one than they were before? For instance, let’s say the player is moving at max speed forwards, but suddenly changes to backwards? Because we ARE still holding a direction, this code applies the acceleration speed where the deceleration speed should probably be applied, resulting in slippery movement when changing directions.
Slipper movement demonstrated:
In this clip I got to full speed in one direction, then immediately reversed direction.