3.1 KinematicBody2D movement only works with CollisionShape as child

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

In Godot 3.1 Beta 1 the move_and_slide function of a KinematicBody2D Node only works when the Node has a CollisionShape as child. But what is if I don’t want to have a collision?

:bust_in_silhouette: Reply From: Zylann

If you don’t want collisions, there is practically no reason to use KinematicBody2D, because without shape, the body has no “existence” in the physics world.

If you don’t need collisions, these two lines do the same:

	# If you want collisions
	move_and_slide(Vector2(100.0, 0.0))

	# If you don't want collisions
	position += Vector2(100.0, 0.0) * delta

Ok this makes sense but I’m not able to get a smooth flappy bird like movement without the move and slide function.
Can you give me a short hint to get a smooth movement without the move and slide function?

882039 | 2019-02-17 00:01

What move_and_slide does without anything to slide on would be this:

position += delta * velocity

Where velocity is a vector you choose for the speed in pixels per second in X and Y, and delta the parameter of the _physics_process function in seconds.

I’m not sure what you mean by move_and_slide not working though. In your case it’s very easy to workaround as you see, but I wonder if that was intentional.

Zylann | 2019-02-17 02:40

There are two different situations:
First situation is that I press a button for e.g. 5 seconds and then the player moves with a constant speed as long as the button is pressed. This works fine.

The second situation is that I press a button just once (not holding it pressed) and then the player “jumps” (like in flappy bird) but the problem here is that player directly teleports to the new position.

882039 | 2019-02-17 11:33

If it teleports at new position there is probably something wrong with your code, even if you used move_and_slide. In a flappy bird game, the “jump” is usually implemented by modifying the velocity, not directly the position (which should be modified every frame in _physics_process, using either move_and_slide or the formula I posted.

Zylann | 2019-02-17 14:27