Sprite keeps on being stuck on the wall

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

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.

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).

eons | 2017-03-14 20:45

Can you post the movement code?

keke | 2017-03-14 23:19

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


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

Code for the player script
if (Input.is_action_pressed(“ui_left”)):
directional_force += DIRECTION.LEFT
direction = “left”
if (Input.is_action_pressed(“ui_right”)):
directional_force += DIRECTION.RIGHT
direction = “right”
if (Input.is_action_pressed(“jump”) && jump_time < TOP_JUMP_TIME && can_jump ):
directional_force += DIRECTION.UP
jump_time += state.get_step()
can_jump = false

ddarkmoto | 2017-03-15 13:57

:bust_in_silhouette: Reply From: PMurrayDesign

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.

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

ddarkmoto | 2017-03-15 13:40

:bust_in_silhouette: Reply From: Peter

Have you set the Friction option to zero?

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

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

ddarkmoto | 2017-03-17 13:30

:bust_in_silhouette: Reply From: Tybobobo

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.

:bust_in_silhouette: Reply From: MrMonk

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