Question on charachtarbody script template

Question

for what purpose exactly is the else statement in the charachtarbody template ?
is it like to ensure the player stops ?

#Get the input direction and handle the movement/deceleration.
#As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector(“left”,“right”,“up”,“down”)
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)

move_and_slide()

Yes you are correct. I agree that code is a bit confusing at first. It is effectively “decreasing” the speed by the amount of SPEED without passing 0 but works for both negative (left, up) movment as well as positive.

In the example code you attached the speed is never larger than SPEED so it could as well set it 0. If speed was higher than SPEED this would slow down player over several frames.

1 Like

ohh i think i get it , its tops the player gradually and cancelling a possible bug.
ty very much mate!

1 Like