diff options
author | Camille Mohr-Daurat <pouleyKetchoup@gmail.com> | 2021-11-19 09:29:53 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-19 09:29:53 -0700 |
commit | 4a127fd2cc16fa426805bf4e08346a1610bf7ce9 (patch) | |
tree | db9dd210b5136eb176f603862cc375243cd5838d | |
parent | 4ab293c13cd230e700e37daf2b75ba74a654a275 (diff) | |
parent | 3a4debfa59978c00607183b1d7b4c82e3ef86e8f (diff) |
Merge pull request #55130 from fabriceci/fix-2d-horizontal-velocity-on-wall
Scales the horizontal velocity according to the wall slope in 2D
-rw-r--r-- | scene/2d/physics_body_2d.cpp | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/scene/2d/physics_body_2d.cpp b/scene/2d/physics_body_2d.cpp index 5ec2a81108..f0e51097db 100644 --- a/scene/2d/physics_body_2d.cpp +++ b/scene/2d/physics_body_2d.cpp @@ -1317,6 +1317,17 @@ void CharacterBody2D::_move_and_slide_grounded(double p_delta, bool p_was_on_flo _snap_on_floor(p_was_on_floor, vel_dir_facing_up); + // Scales the horizontal velocity according to the wall slope. + if (is_on_wall_only() && motion_slide_up.dot(motion_results.get(0).collision_normal) < 0) { + Vector2 slide_motion = motion_velocity.slide(motion_results.get(0).collision_normal); + if (motion_slide_up.dot(slide_motion) < 0) { + motion_velocity = up_direction * up_direction.dot(motion_velocity); + } else { + // Keeps the vertical motion from motion_velocity and add the horizontal motion of the projection. + motion_velocity = up_direction * up_direction.dot(motion_velocity) + slide_motion.slide(up_direction); + } + } + // Reset the gravity accumulation when touching the ground. if (on_floor && !vel_dir_facing_up) { motion_velocity = motion_velocity.slide(up_direction); |