0 votes

I'm trying to use the Godot 2D physics (a RigidBody2D to be more specific) to create a retro arcade platformer, but the one problem I can't seem to solve or find a solution for anywhere is to reset the player's horizontal velocity back to 0.
Please don't judge me for my terribly sloppy code, I'm new to python-like coding. :)
My code for moving the player left and right:

if not_moving:
    print('NOT MOVING')

    if (go_left and not go_right):
    if (go_right and not go_left):

The notmoving variable is defined early in my code as being true "if not (goleft or goright)"
I know for a fact my not
moving variable works for detecting when neither the left or right arrow key is pressed because I set it to print to the console.

The goleft and goright variables are working too, because pressing the respective arrow key will apply the velocity.

The only thing is, when I release both arrow keys, the player just keeps moving in the last pressed direction. I'm trying to create a platformer with very tight controls, so this doesn't bode well at all. If anyone can help, please do!

Thanks in advance,

in Engine by (19 points)

3 Answers

0 votes
Best answer

Quick Update; I found a fix to this problem, so I'll write it here for any people with this problem in the future. Apparently (or so I conjecture), setting either of the Vector2 values at 0 for the setaxisvelocity function will make them not change. So, in order to make it not 0, I just set it to be "0.001". This may not be the best fix but it works.

In short, just change the Vector2 value you're trying to reset to something extremely low like 0.001 to fix the problem.

by (19 points)

You can increase the damp or use _integrate_forces (custom integrator) for a more control of the rigid behavior too.

Check platformer demo, it uses the custom integrator to modify the rigid body state.

+1 vote

The proper way to control simulation is by using the simulation. If you directly set variables like velocity you are working against the physics instead of controlling it.
To null a force you just have to apply an opposite one.
Inertia is the resistance in changing movement so the opposite force you have to apply to archive 0 velocity using the simulation itself is "-current_velocity * inertia"

by (23 points)
edited by
0 votes

Get! Do! Set!

# Get!
var lv = get_linear_velocity()

# Do!
if LEFT and not RIGHT:
    lv.x = -movespeed
elif RIGHT and not LEFT:
    lv.x = movespeed
    lv.x = 0

# Set!
if lv != get_linear_velocity():
by (1,328 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.