diff --git a/scenes/game_logic/walk_behaviors/input_walk_behavior.gd b/scenes/game_logic/walk_behaviors/input_walk_behavior.gd index 0c8c35027f..116d58f8c8 100644 --- a/scenes/game_logic/walk_behaviors/input_walk_behavior.gd +++ b/scenes/game_logic/walk_behaviors/input_walk_behavior.gd @@ -21,6 +21,9 @@ var input_vector: Vector2 var is_running: bool: set = _set_is_running +var _is_stuck: bool +var _last_safe_position: Vector2 + func _set_is_running(new_is_running: bool) -> void: if is_running == new_is_running: @@ -53,6 +56,15 @@ func _physics_process(delta: float) -> void: character.velocity = character.velocity.move_toward(input_vector, step * delta) character.move_and_slide() + # Check if the player got stuck (is colliding after the move_and_slide). + var collision := character.move_and_collide(Vector2.ZERO, true) + _is_stuck = collision != null + if _is_stuck: + character.velocity = Vector2.ZERO + character.position = _last_safe_position + else: + _last_safe_position = character.global_position + # When using an analogue joystick, this can be false even if the player is # holding the "run" button, because the joystick may be inclined only slightly. is_running = input_vector.length_squared() > (speeds.walk_speed * speeds.walk_speed) + 1.0