CharacterBody2D collides when it should slide

Godot Version



Hello, guys. I’m implementing a simple demo where the player can kick blocks which should slide until they either hit a wall or another block. The blocks were implemented as CharacterBody2D, since I only plan on moving them through code. I’ve placed them using a grid, but when the player kicks a block that should slide besides another block, it instead collides. Both player and blocks have the motion mode set to “Floating” and the floor layers for them have been all disabled.

The project is available on GitHub. I’ve tried setting a safe margin, switching between move_and_collide and move_and_slide and adjusting other CharacterBody2D settings with no success. Does anyone know what I’m missing here?

Here is the code for the block:

extends CharacterBody2D

const SPEED := 500

func _physics_process(delta: float) -> void:
	var collision = move_and_collide(velocity * delta)
	if collision:
		velocity = Vector2.ZERO

func kick(direction: String) -> void:
	if direction == "up":
		velocity = Vector2(0, -SPEED)
	elif direction == "down":
		velocity = Vector2(0, SPEED)
	if direction == "right":
		velocity = Vector2(SPEED,0)
	elif direction == "left":
		velocity = Vector2(-SPEED,0)

Here’s a video demonstrating the issue:

PhysicsBody2D.move_and_collide() won’t slide when a collision happens. You need to use CharacterBody2D.move_and_slide()

1 Like

I did try move_and_slide, if I replace my existing _physics_process function with:

func _physics_process(delta: float) -> void:

The behavior in that scenario remains the same.

Try making the CollisionShape2D of the block a bit smaller then.

That doesn’t really help me much, since it fixes this particular minimal scenario but won’t work for cases where I want more interactions to happen, like the player kicking a block against another one and kicking it again from that position. I would end up with the same issue since anytime a block should slide right past another it would get stuck.