Can't break away from a moving wall when using move_and_slide()

This does work. Thank you very much for your answer!Since it’s a top-down 2D project where the character can rotate and the wall’s direction is not fixed, the problem of being unable to break away still occurs when the collision shape’s angle changes. Is there a better way to handle this? Or should I wait for a future version of Godot to fix this bug?

If you want it to be a circle, then I’d recommend changing your player to a RigidBody2D and using this code instead:

extends RigidBody2D

@export var speed : float = 300.0


func _physics_process(_delta: float) -> void:
	var direction := Input.get_vector("move_left", "move_right", "move_up", "move_down")
	if direction:
		constant_force = direction * speed
	else:
		constant_force = Vector2.ZERO

You’re also going to have to make the following changes in the Inspector:

And it’s going to move differently - a little sluggish at first.

1 Like

I’ve found a workaround. I’m not sure if it’s stable, but I’ve tested it with walls at different angles and a player with circular collision, and it works. Could you help me check if this is viable?

extends Node2D

var player_speed: float = 300.0
var wall_speed: float = 100.0

@onready var wall: AnimatableBody2D = $Wall
@onready var player: CharacterBody2D = $Player

func _physics_process(delta: float) -> void:
	wall.global_position.x += wall_speed * delta
	
	var input_dir: Vector2 = Input.get_vector("move_left", "move_right", "move_up", "move_down")
	var collision = player.move_and_collide(input_dir * player_speed * delta, true)
	if collision:
		player.move_and_collide(collision.get_travel())
		if collision.get_remainder():
			player.velocity = collision.get_remainder() / delta
			player.move_and_slide()
	else:
		player.move_and_collide(input_dir * player_speed * delta)

Sorry, this method fails when the wall’s speed is too high, for example 500

1 Like