How to correctly apply velocity a RigidBody2D?

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

I’m in a hard situation. To manage movement in monster and players, I use a RigidBody2D with no friction. Because of that I need to set it’s X velocity to 0 every frame when not walking. But that means I can’t apply other forces to it, because it’s gonna be set to 0. How did you manage it in your game? I’m thinking in creating an array of forces to be applied every frame, is that the best option?

If you want to apply forces to your player/monster and don’t want it to move, resulting forces in the direction of movement must be zero. It’s Newton’s second law consequence.
Why don’t simply change your player concept and leave it move with friction? If no friction, the the player has to be stopped by user interaction (i.e. a button/action to stop it)

For monster I wouldn’t use RigidBody2D, better KinematicBody2D

genete | 2016-09-03 22:51

Don’t set it’s X velocity to 0 every frame. If you want the character to stop instantly when not walking you should be able to apply an appropriate force in the opposite direction once to stop it. Or if you’re OK with it taking a little time to stop, just make your own friction/damping. I assume you’re detecting when the characters are on the ground or not. When it is on the ground and not walking, apply a force to stop it, (-velocity.x * mass * k(damping coefficient)). Adjust k until it’s as slidy/sticky as you want.

rgrams | 2016-09-08 01:08