0 votes

I have a KinematicBody2D, to move it I use the function move_and_slide(speed), where speed is a Vector2, and it has simple gravity:

var spd = 500
var acceleration = 10
var jmpSpd = 800
var GRAVITY = 1300
var speed = Vector2()

func _ready():

func _process(delta):
    if Input.is_action_pressed("btn_jump") and is_on_floor():
        speed.y = -jmpSpd

    var desiredSpeed = 0
    if Input.is_action_pressed("btn_right"):
        desiredSpeed = spd
    elif Input.is_action_pressed("btn_left"):
        desiredSpeed = -spd
    speed.x += (desiredSpeed - speed.x)*acceleration*delta
    speed.y += GRAVITY*delta

    speed = move_and_slide(speed)

The problem is that sometimes the player jumps higher than it should, and falls faster, and moves faster, this generally only happens when I have just turned on my computer, which makes me think that it has something to do with the number of times _process(delta) is called, but I have no idea.
Maybe I should use move_and_slide(speed*delta), but then I would need very big values for all the variables, and when using move_and_slide(speed) I get results much more similar to Godot 2 (this tutorial).

What should I do?

Thanks in advance

in Engine by (127 points)
edited by

1 Answer

+4 votes
Best answer

Two problems:

1) You should not be using move_and_slide in _process(). Use _physics_process().
2) move_and_slide() uses pixels per second, so you should not multiply by delta

Per the docs for KinematicBody2D:

linearvelocity is a value in pixels per second. Unlike in for example moveand_collide, you should not multiply it with delta — this is done by the method.

by (21,313 points)
selected by

Thanks! Now everything works out.

Thanks for this. I had the same problem (called move_and_slide in _process instead of _process_physics) and everything worked fine ... except on one dev machine (laptop) where the player moved in slow motion.

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 webmaster@godotengine.org with your username.