From 825bc032dac85ba5b9ff519a2052302617af0216 Mon Sep 17 00:00:00 2001 From: karl Date: Mon, 7 Jun 2021 19:02:24 +0200 Subject: [PATCH] Attempt at making grounded movement work well --- Player.gd | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Player.gd b/Player.gd index 60b2890..f0cd525 100644 --- a/Player.gd +++ b/Player.gd @@ -63,8 +63,13 @@ func _physics_process(delta): # Make movement local move_acceleration = transform.basis * move_acceleration + print(on_ground) + # Jumping and Gravity var gravity_acceleration = get_node(solar_system).get_gravitation_acceleration(transform.origin) + #if on_ground: + # FIXME: This breaks some things, but seems to be the right principle + #gravity_acceleration = Vector3.ZERO apply_acceleration((move_acceleration + gravity_acceleration) * delta) @@ -80,17 +85,21 @@ func _physics_process(delta): time_since_jump_start += delta # Apply movement to position - # FIXME: move_and_slide might make more sense, but couldn't quite get that to work... - #move_and_slide(velocity, -gravity_acceleration.normalized(), true) - var collision = move_and_collide(velocity * delta) + velocity = move_and_slide(velocity, -gravity_acceleration.normalized(), true) + on_ground = false - if collision: + if get_slide_count() > 0: + var collision = get_slide_collision(0) + if collision.collider.is_in_group("MovingPlatform"): - # Inherit the moving platform's velocity - current_target_velocity = collision.collider.velocity - velocity = current_target_velocity + # Inherit the moving platform's velocity and apply the initial velocity + if current_target_velocity != collision.collider.velocity / 6.0: + current_target_velocity = collision.collider.velocity / 6.0 + velocity = current_target_velocity if not jumping: - on_ground = true + # Check the collision normal and set to grounded if it's close to the player's up vector + if collision.normal.dot(global_transform.basis.y) > 0.5: # an angle of >45° counts as grounded + on_ground = true else: # We're not colliding with anything, so drag takes us towards 0 current_target_velocity = Vector3.ZERO