Rigidbody acts weirdly

Godot Version

4.4.1

Question

I was working on a box that my character can push when I encountered this bug

for some reason the rigidbody goes insanely fast and I can’t figure out why it seems to work just fine but when I jump then push the rigidbody while not on ground it sometimes goes very fast sometimes doesn’t although there isn’t anything in the scene except staticbody, rigidbody and characterbody with all their default values and a script handling movement and pushing

I noticed that the linearvelocity of the rigidbody becomes very high when the bug occurs as well as my character going with the rigidbody but no actual difference in the character velocity

here is the script for my character

extends CharacterBody2D

const SPEED: float = 300.0
const DEACCELERATION: float = 50.0
const JUMP_VELOCITY: float = -550.0
const PUSH_POWER: float = 60.0

func _physics_process(delta: float) -> void:
	if not is_on_floor():
		velocity += get_gravity() * delta

	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	var direction = Input.get_axis("left", "right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, DEACCELERATION)
	
	move_and_slide()
	
	detect_box_and_push()
	
func detect_box_and_push() -> void:
	for index in get_slide_collision_count():
		var collision = get_slide_collision(index)
		var collider = collision.get_collider()
		if collider is RigidBody2D:
			#if not is_on_floor(): return
			var normal = collision.get_normal()
			collider.apply_central_impulse(-normal * 30)

I found a solution which is to prevent pushing if not on ground but I would like to know an explanation to why this is happening

one last thing I noticed is that pushing while in air is stronger than on the ground I tried putting two boxes beside each other, pushing on the ground does nothing while on air pushes both of them

I am still new to godot so there might be something I am missing causing this

Any help is appreciated

It look like your CharacterBody2D is accelerating at the same rate as the RigidBody2D, which suggests to me this may be a complication caused by the “On Leave” property of your player’s Moving Platform settings.

By default, grounded CharacterBody2Ds match the velocity of a moving object they are on, and maintain that velocity when they stop colliding with it. Since the RigidBody2D is trying to move away from it, your player’s velocity will keep increasing to match the box, and the box’s velocity will keep increasing to get away from the player.

The solution is to disable “Floor Layer” collision in the CharacterBody2D on any layers the RigidBody2D uses. This will not disable regular collision, just “moving platform collision” that applies velocity to the player.