Make a RigidBody2D player move smoothly with the floor

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

Sometimes when my character is walking on the floor, it appears like there is some sort of invisible bump on the floor. If i turn character mode on, the player stops moving when hitting that ‘‘rough’’ spot on the floor, when on rigid mode, it rotates like if it’s hitting a bump. The collision boxes for the tileset and the player are completely straight.

I can’t understand what the problem with this is.

Is there a reason you couldn’t use a kinematic body for your player instead?

happycamper | 2018-06-02 03:27

Yes, the player is supposed to have the same physics as all the objects in the game. In fact it’s on rigid mode instead of character.

SqrtTwo | 2018-06-03 00:14

What method are you using to update the movement? Do you have a code sample?

alfandango | 2018-06-03 23:55

The left-right movement is pretty simple, it’s constant, no acceleration as for yet. Nothing but this simple 4 lined code:

if Input.is_action_pressed('ui_right'):
		motion.x = SPEED
elif Input.is_action_pressed('ui_left'):
		motion.x = -SPEED

SqrtTwo | 2018-06-10 01:03

:bust_in_silhouette: Reply From: alfandango

The docs advise against the use of RigidBody2d for your character and if you still wish to use it, you should not simulate movement by changing the position of the object…

[…] if you do wish to have some control over the body, you should take care - altering the position, linear_velocity, or other physics properties of a rigid body can result in unexpected behavior. If you need to alter any of the physics-related properties, you should use the _integrate_forces() callback instead of _physics_process().


Instead you would be much better off using KinematicBody2D for your character and movement.

See Using KinematicBody2D and Kinematic Character (2D)

Hope that helps.

I’m not making a standard platformer, i want my character to behave like a normal object affected by the same physics as all the other rigid bodies in the game, so that’s why i made it a rigid instead of a kinematic.

SqrtTwo | 2018-06-15 01:31

:bust_in_silhouette: Reply From: Anaxagor

Are you using a TileMap? Tiles each have individual collision shapes which creates problems with collisions, from what I understand. When your character collides with the floor where two tiles are touching, you will often get two collisions. One of these will be with the corner of one of the tiles! The normal of that collision will be very different from the floor normal (often 0,-1).

Another way to test and see this is to have a RigidBody2D with bounce = 1 and a CircleShape2D as collision shape, and just look at it bounce on your TileMap floor: it will often do a bad bounce for no apparent reason. With Debug → Visible Collision Shapes you will see two red dots (collisions) because it collides with two tiles: the top of one and the corner of the other. I have no idea how to fix this ATM. I hope I’m getting something wrong…

:bust_in_silhouette: Reply From: gnumaru

The character is getting stuck or rotating because you are using a rectangle collision shape and the sides of the lower corners of the rectangle are colliding with the sides of the upper corners of the floor (since tileset collisions create several square shapes). A workaround for this is using circle shapes for the feet of the caracter.

I forgot about capsuleshape. It should be ideal to solve this problem since it gives you a roughly rectangular shape for the character but with round feet.

gnumaru | 2020-01-24 18:03