Player keeps sliding/moving even when movement buttons aren't pressed.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ItzKillerTTV

When starting to implement moving mechanics I realized that my character keeps sliding rapidly even though I specified a friction variable. I’m new to programming in general, so any help would be appreciated. If you need anything else from the code of the project let me know.

Here’s the code:


extends KinematicBody2D
class_name Character, "res://extras/sprite1.png"

const FRICTION = 0.15
export(int) var acceleration: int = 90
export(int) var max_speed: int = 500 

onready var animated_sprite: AnimatedSprite = get_node("AnimatedSprite")
var mov_direction: Vector2 = Vector2.ZERO
var velocity: Vector2 = Vector2.ZERO

func _physics_process(_delta: float) -> void:
    velocity = move_and_slide(velocity)
	velocity = lerp(velocity, Vector2.ZERO, FRICTION)
	$AnimatedSprite.play("Idle")

func move() -> void:
	mov_direction = mov_direction.normalized()
	velocity = mov_direction * acceleration
 	velocity = velocity.clamped(max_speed)
:bust_in_silhouette: Reply From: BoxyLlama

Is the player moving forever, or just slowing down very very slowly?

  • One way to test this, using a print(velocity) statement to print the velocity just before your move_and_slide function. Is it decreasing at all, or remaining constant?

If velocity is remaining Constant:

  • Make sure you’re only calling the move() function, when you want to be moving (i.e. holding the button)
    • One way to test this, would be to put a print("moving") statement in there, to make sure it’s only printing when you’re holding the button. If it’s printing when you’re not holding the button. You’d want to make sure your conditions are correct.

If velocity is decreasing very very slowly:

  • Try increasing your Friction. In my test, I use a much greater friction value. My acceleration is 30, and my Friction is 50. 0.15 might just be too low. If I had to guess, this is the problem.