0 votes

Hi everyone. It seems that every time I press the left and right movement button, the sprite keeps on sticking on the edge of a staticbody2d for as long I press them. Thoughts? Thank you.

enter image description here

in Engine by (44 points)

Add more details on how are you moving the rigid body, if you keep adding impulse/forces and has a high friction, is normal for it to get stuck (like in real physics).

Can you post the movement code?

Hi Keke and eons.

Here's the code for my movement
charmovement script

const DIRECTION = {
ZERO = Vector2(0,0),
LEFT = Vector2(-1, 0),
RIGHT = Vector2(1, 0),
UP = Vector2(0, -1),
DOWN = Vector2(0, 1)
}

func _integrate_forces(state):
#final force
var final_force = Vector2()

directional_force = DIRECTION.ZERO

apply_force(state)

final_force =state.get_linear_velocity() + (directional_force * acceleration)

#prevent from exceeding top speed
if (final_force.x > top_move_speed):
    final_force.x = top_move_speed
elif (final_force.x < -top_move_speed):
    final_force.x = -top_move_speed

if (final_force.y > top_jump_speed):
    final_force.y = top_jump_speed
elif (final_force.y < -top_jump_speed):
    final_force.y = -top_jump_speed

#add force
state.set_linear_velocity(final_force)

Code for the player script
if (Input.isactionpressed("uileft")):
directional
force += DIRECTION.LEFT
direction = "left"
print(directionalforce)
if (Input.is
actionpressed("uiright")):
directionalforce += DIRECTION.RIGHT
direction = "right"
print(directional
force)
if (Input.isactionpressed("jump") && jumptime < TOPJUMPTIME && canjump ):
directionalforce += DIRECTION.UP
jump
time += state.getstep()
else:
can
jump = false

4 Answers

–2 votes

Try using a round collision shape, rather than a capsule. The flat edges basically have friction, hence why you're sticking. With a circular shape, you should be ok.

by (108 points)

Tried it with different shapes and it didn't work but thanks for the suggestion. Appreciate it :)

0 votes

Have you set the Friction option to zero?

In my case, RigidBody2D objects will stick on collision fields, like ground...

by (26 points)

Yes sir. I tried and it didn't work.

0 votes

I noticed you seem to be using a collision check on the bottom of your player. If that is the case, you could disable friction on the body while in air, and turn it on as you land.

Of course, I do not know the extent of your project, so it will be up to you to determine if this would be a viable solution for you.

by (495 points)
0 votes

I have two suggestions:
1. use CollisionPolygon2d, as the shape for you body, but I think is related to the physics of the body as eons pointed out. If it's physics, then if you increase gravity to a value higher than the right force applied it should start slowly moving down.
2. unrelated use clamp ( float val, float min, float max ) instead of the if's

by (528 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 webmaster@godotengine.org with your username.