Essentially, I followed this tutorial on how to push a RigidBody2D with a CharacterBody2D, the only difference being the name of the variable to determine the push force. Seemingly at random, the crate will stop moving, and is only able to move again when I push from the other side. Below is my code in my player object script (including entire player movement just in case that's the issue):

const PUSH = 100

func _physics_process(delta):
    # Movement
	var direction = Input.get_axis("left", "right")
	if direction:
		if Input.is_action_pressed("run"):
			velocity.x = direction * RUN
		else:
			velocity.x = direction * WALK
		if is_on_floor():
			sprite.play("running" + str(global.character))
	else:
		velocity.x = move_toward(velocity.x, 0, 20)
		if is_on_floor():
			sprite.play("default" + str(global.character))
		
	# Sprite Direction
	if direction > 0:
		sprite.flip_h = false
	elif direction < 0:
		sprite.flip_h = true
		
	# Gravity
	if not is_on_floor():
		velocity.y += GRAVITY * delta
		if velocity.y > 0:
			sprite.play("falling" + str(global.character))
			
	# Jumping/Drop
	if Input.is_action_just_pressed("jump") and is_on_floor():
		if Input.is_action_pressed("down"):
			position.y += 15
		else:
			velocity.y = JUMP_VELOCITY
			sprite.play("jumping" + str(global.character))

    move_and_slide():

    for i in get_slide_collision_count():
	    var c = get_slide_collision(i)
	    if c.get_collider() is RigidBody2D:
		    c.get_collider().apply_central_impulse(-c.get_normal() * PUSH)

The crate has an _integrate_forces function that caps the x velocity to a max speed (this "getting stuck" issue was a problem before implementing that, but I'll add it just in case)

var maxSpeed = 500

func _integrate_forces(_state):
	if linear_velocity.x > maxSpeed or linear_velocity.x < -maxSpeed:
		var newSpeed = linear_velocity.normalized()
		newSpeed *= maxSpeed
		linear_velocity = newSpeed
	print(linear_velocity)

The physics settings for the crate are shown on the Inspector tab, and the velocity is shown on the Output. I'm still new to RigidBodies and Godot physics, and everything I've searched led back to a variation of the original tutorial's code. Does anyone at least have an idea of what's causing this and how I can adjust it?

  • xyz replied to this.

    Awsome2464 For start, print out all relevant info for all slide collisions each frame and compare if something different is happening there when the crate is stuck. While hunting this down, remove custom force integration just so the problem is more narrowly contained.