Move and slide gives lagging behaviour

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

I am writing a very small project and I use move_and_slide to move my character. But somehow it feels very “lagging” (allthough on the FPS counter you can see that it isn’t actually lagging). It looks as if it collides with something for quite a while and after some time somehow resolves the collision. In the example below I always press right all the time, then left all the time, then right all the time etc. The moments where he halts I do not let go of the movement key and keep it pressed until it moves again.
Here is a gif of the behaviour:
(I tried to inline it but I had no luck, sorry)
Does anyone know what could be wrong?

EDIT: Full project, zipped:
The part where I use move_and_slide, in my KinematicsBody2D node:

var velocity = Vector2()
func _physics_process(delta):
    velocity.y += delta * GRAVITY
    if Input.is_action_pressed("ui_left"):
	    velocity.x = -WALK_SPEED
    elif Input.is_action_pressed("ui_right"):
	    velocity.x =  WALK_SPEED
	    velocity.x = 0

    move_and_slide(velocity, Vector2(0, -1))``

It’s hard to tell you what may be wrong without seeing your code. Please post the code for the player where you’re using move_and_slide().

kidscancode | 2018-02-22 01:29

thank you for answering, I edited my question

wentdot | 2018-02-22 08:57

:bust_in_silhouette: Reply From: gtkampos

In the last line of code:


 move_and_slide(velocity, Vector2(0, -1))


velocity = move_and_slide(velocity, Vector2(0, -1))

Thank you, I thought that move_and_slide just does the “corrected” movement and does not needs to update the velocity. Your tip already helped, but now he randomly jumps. Does that mean that my collision shapes are not perfect and there are some small bumps on the ground?

wentdot | 2018-02-23 16:08

You could check these things:

  • The collision shapes are in scale 1 by 1. You CANT scale the collision NODE. You have to use the inner handles to “scale” the size of the SHAPE (again, not the node) to your desire.

  • See if the shape you are walking has the bouciness 0.

  • How are you making the level collision shape? Collision shape to each tile? Check if there is no gap between the tiles. You can use pixel snape when “scaling” the shapes, so it will help to fit exactly your tiles (better if it is pixel art)

gtkampos | 2018-02-23 16:30