• 2D
  • Two Questions on move_and_slide (collision)

Hi All,

I've got two questions that I hope you guys can help me with. First, let me show you what I have:

One KinematicBody2D with a Label attached to it (let's call this the Player). The ground is a StaticBody2D (I'm showing the collision shapes)

The Label shows the name of the object the KinematicBody2D is colliding with as per below:

move_and_slide(motion, UP)
if get_slide_count()==1:
	$Label.text = get_slide_collision(0).get_collider().name
	if get_slide_count()=2:
		$Label.text = get_slide_collision(0).get_collider().name +"/"+ 	get_slide_collision(1).get_collider().name

The above is inside the Player's _physics_process.

Question 1: When I move my KinematicBody2D (and only when I move) I get:

ie. There are two collisions with the same ground in the same _physics_process. Is this normal?? (I ask because it may be related to the bug below)

Question 2: If I have the Player (KinematicBody2d + debug label + Gravity) and another KinematicBody2D (let's call it Fly2, it follows the player), I get this:

ie. The player collides with Fly2 twice in the same _physics_process. Also, they keep jittering and the player is kind of trapped (I have to move it using the keyboard a few times before it actually moves). Why is this happening (notice they overlap a bit, I guess this is related) and how do I fix it?

Bonus question: how do I put tabs in a code tag in this Markdown? (so that my ifs are correct in the future)

Thanks!!!

Are you applying gravity to the player? It could be that the move_and_slide function is returning two slide collisions because one is for colliding with the floor on the Y axis, and then another for sliding across the floor on the X axis (as the Y velocity is converted due to the slide).

That said, if you are not moving the player and only applying gravity, there should only be a single collision slide, so maybe it is a bug? I do not do a lot of 2D work with Godot, so maybe this is expected behavior.

As for the second, I know I’ve seen this before, but I don’t remember what the solution was. I believe this happens when two KinematicBody2D nodes are moving in the same direction at the same time while beside each other.

What is the motion vector when the KinematicBody2D nodes are colliding? Also, do the two KinematicBody2D nodes start next to each other, or do you get the result in the second picture when you move them together?

Hopefully this helps! (Side note: Welcome to the forums!)

Bonus question: How do I put tabs in a code tag in this Markdown?

You don’t need code tags, just a single tab character will render the text as code. You can also select code from the format dropdown while your code is selected :smile:

As for inserting tabs, normally you just need to press the tab character on the keyboard, however sometimes (like on mobile) this will bring you straight to the tags field. In this case, I’ve found it easiest to copy a tab character from another document and paste it where needed.

I fixed the formatting on your post, so if you want you can edit the post to see how I changed it.

4 days later

Yes, I'm applying gravity and your answer does make sense - thanks!

If anybody else has the same problem, here is a solution:

if (TargetBody.position - position).length() > 130:
	Velocity = move_and_slide(Velocity)

I put the above in the code of my enemy. This way it only moves if there is a certain distance between it and the player. You'll need to play a bit with the constant (130 in my case) so that it's high enough to stop the problem but not so high that collision does not happen anymore.