0 votes

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)

func move() -> void:
    mov_direction = mov_direction.normalized()
    velocity = mov_direction * acceleration
    velocity = velocity.clamped(max_speed)
Godot version v3.2.3
in Engine by (35 points)

1 Answer

+1 vote
Best answer

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.
by (336 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.