(Answered) Problem with move_and_slide() and framerate [Godot 3 beta 2]

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PugMasterPug
:warning: Old Version Published before Godot 3 was released.

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():
    set_process(true)

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

:bust_in_silhouette: Reply From: kidscancode

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:

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

Thanks! Now everything works out.

PugMasterPug | 2018-01-10 20:50

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.

nightblade9 | 2018-12-16 23:10