The player gets stuck when colliding with a rigidbody 2d

Hello, good afternoon, I have a strange error. Every time my player moves a Rigidbody2D from the top, it gets ‘hooked’ to the object and it starts to go down even though I’m not moving the player.

Could you possibly give more context on the situation? Maybe a screenshot and the code you use in the scripts?
This way more people could help you :slight_smile:

Hello. I don’t know how to do it because I try to post it and it tells me that ‘Sorry, new users cannot upload attachments. Oh yes, here is the script: func Push_Rigidbody(): for i in range(get_slide_collision_count()): var col = get_slide_collision(i) if col.get_collider() is RigidBody2D: rigid_body = col.get_collider() col.get_collider().apply_force(col.get_normal() * fuerza) if rigid_body != null: rigid_body.linear_damp = amortiguacion

It might be that your account level is just too low to put pictures in…
Maybe you could try to implement the move_and_slide()
function which basically changes the movement a liitle bit.

I’m not understanding how I can implement move_and_slide(). Ah, now I can post videos and here I put it for you to see:

To implement move and slide, you just put it at the end of your process function.
Just add move_and_slide()
Sorry for the late answer

Hello again, look, I tried what you told me about the move_and_slide but this worsens the bug (although of course there is the detail that I increased the push force from 10 to 100 otherwise it was not going to move) here I attach the code and a video: `extends CharacterBody2D

@onready var Raycast = $RayCast2D
@onready var Player = $CollisionShape2D
const velocidad = 100.0
var fuerza = -100
var friccion = 2
var amortiguacion = 15
var rigid_body
var colicion_ray
var Direccion

func _physics_process(_delta):
Direccion = Input.get_vector(“Mover izquierda”,“Mover derecha”,“Mover arriba”,“Mover abajo”)
velocity = Direccion * velocidad
if Direccion != Vector2 (0, 0):
Raycast.target_position.x = Direccion.x * 3
Raycast.target_position.y = Direccion.y * 3
Movimiento_Rigidbody()
Rotacion()
move_and_slide()

func _process(_delta):
move_and_slide()

#region funciones
func Movimiento_Rigidbody():
var _velocidad = Vector2() # Define tu velocidad aquí
for i in range(get_slide_collision_count()):
var col = get_slide_collision(i)
if col != null and col.get_collider() is RigidBody2D:
rigid_body = col.get_collider()
col.get_collider().apply_force(col.get_normal() * fuerza)
print(“A”)
if rigid_body != null:
rigid_body.linear_damp = amortiguacion
rigid_body.move_and_collide(_velocidad)

func Rotacion():
colicion_ray = Raycast.get_collider ()
if colicion_ray != null and colicion_ray.is_in_group(“objecto rotable”):
colicion_ray._Rotacion()
#endredion`

You only need to add it once to the player script, not twice in the same script. Other than that, you could try putting move and slide into the box script.

If that doesn’t work, then I will be sorry to tell you that I have almost no idea of how to solve this.
One last thing I could do is to copy the whole code and try to look for the bug source myself, but I don’t think I would find much

If I’m going to try to see what causes this, although I don’t know where the error is coming from because I’ve been searching in various places and they implement code almost identical to mine, but they don’t encounter this bug. Anyway, thank you for your help.

No big deal, maybe you will find the solution on a random occasion or just randomly implement an idea that solves the problem.
Hope you have a great day!

Hello again, it’s been a while. These past few weeks, I’ve been searching for a solution, and I found one, although it’s somewhat complex. In summary, the player’s character body should have a raycast that points in the direction they’re moving. Next, create a 2D area with collision detection. Finally, add a line of code that sends a signal to the rigidbody (passing a variable indicating which buttons are being pressed to move it in the opposite direction). Once in the rigidbody, implement a small function for how it should move. Additionally, if you want to limit how far it goes when you press it, set ‘linear_damp’ in the func _process(_delta) function. Below, I’ve included the code for anyone who needs it, although it’s quite basic and could benefit from review and improvement (especially since this code works well in my project, where the rigidbody rotation function is already implemented):
func _physics_process(_delta):
Direccion = Input.get_vector(“Mover izquierda”,“Mover derecha”,“Mover arriba”,“Mover abajo”)
velocity = Direccion * velocidad
if Direccion != Vector2 (0, 0):
Raycast.target_position.x = Direccion.x * 3
Raycast.target_position.y = Direccion.y * 3
Area2d.position = Direccion * 8
if Direccion.x != 0:
Collicion.shape.extents = Vector2(8, 9)
elif Direccion.y != 0:
Collicion.shape.extents = Vector2(9, 8)
Rotacion_y_Movimiento()
move_and_slide()

func _process(_delta):
Rotacion_boton = Input.get_axis(“Rotar izquierda”,“Rotar derecha”)

func _on_area_2d_body_entered(_body):
tof = true

func _on_area_2d_body_exited(_body):
tof = false

#region funciones

func Rotacion_y_Movimiento():

colicion_ray = Raycast.get_collider ()
if colicion_ray != null:
	if colicion_ray.has_method("_Rotacion"):
		colicion_ray._Rotacion(Rotacion_boton)
	if colicion_ray.is_in_group("objecto empujable") and tof == true:
		colicion_ray._Movimento(Direccion)
1 Like

Awesome, nice to see that you made it!