From 72650f9787f880353245a0630ba5ba25f814fe71 Mon Sep 17 00:00:00 2001 From: YoSoyFreeman Date: Mon, 18 Nov 2024 15:53:52 +0100 Subject: [PATCH] Allow apply_floor_snap to preserve the horizontal position after the snapping independently of stop_on_slopes --- scene/2d/physics/character_body_2d.cpp | 16 ++++++++-------- scene/3d/physics/character_body_3d.cpp | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/scene/2d/physics/character_body_2d.cpp b/scene/2d/physics/character_body_2d.cpp index a503f3cb78e..32582cc1209 100644 --- a/scene/2d/physics/character_body_2d.cpp +++ b/scene/2d/physics/character_body_2d.cpp @@ -352,14 +352,14 @@ void CharacterBody2D::_apply_floor_snap(bool p_wall_as_floor) { floor_normal = result.collision_normal; _set_platform_data(result); - if (floor_stop_on_slope) { - // move and collide may stray the object a bit because of pre un-stucking, - // so only ensure that motion happens on floor direction in this case. - if (result.travel.length() > margin) { - result.travel = up_direction * up_direction.dot(result.travel); - } else { - result.travel = Vector2(); - } + // Ensure that we only move the body along the up axis, because + // move_and_collide may stray the object a bit when getting it unstuck. + // Canceling this motion should not affect move_and_slide, as previous + // calls to move_and_collide already took care of freeing the body. + if (result.travel.length() > margin) { + result.travel = up_direction * up_direction.dot(result.travel); + } else { + result.travel = Vector2(); } parameters.from.columns[2] += result.travel; diff --git a/scene/3d/physics/character_body_3d.cpp b/scene/3d/physics/character_body_3d.cpp index e3815e82190..1aea2cbd9ec 100644 --- a/scene/3d/physics/character_body_3d.cpp +++ b/scene/3d/physics/character_body_3d.cpp @@ -469,14 +469,14 @@ void CharacterBody3D::apply_floor_snap() { _set_collision_direction(result, result_state, CollisionState(true, false, false)); if (result_state.floor) { - if (floor_stop_on_slope) { - // move and collide may stray the object a bit because of pre un-stucking, - // so only ensure that motion happens on floor direction in this case. - if (result.travel.length() > margin) { - result.travel = up_direction * up_direction.dot(result.travel); - } else { - result.travel = Vector3(); - } + // Ensure that we only move the body along the up axis, because + // move_and_collide may stray the object a bit when getting it unstuck. + // Canceling this motion should not affect move_and_slide, as previous + // calls to move_and_collide already took care of freeing the body. + if (result.travel.length() > margin) { + result.travel = up_direction * up_direction.dot(result.travel); + } else { + result.travel = Vector3(); } parameters.from.origin += result.travel;