diff --git a/Player.gd b/Player.gd index 0d7c49d..1a1de58 100644 --- a/Player.gd +++ b/Player.gd @@ -20,6 +20,7 @@ export var jump_exponent = 0.05 export var almost_on_ground_length = 1.0 var current_target_velocity := Vector3.ZERO +var has_inherited_velocity := false export(NodePath) var solar_system @@ -84,6 +85,27 @@ func is_on_ground(): return false +# Returns true if the player is (almost) on the ground and that ground is a moving platform. +func is_on_moving_platform(): + return is_on_ground() and $GroundCheckRay.get_collider().is_in_group("MovingPlatform") + + +# Set the current target velocity to the moving platform that is currently below the player. +# If this was the first collision frame, also inherit the platform's velocity. +func apply_moving_platform_velocity(): + if not has_inherited_velocity: + velocity += $GroundCheckRay.get_collider().velocity + has_inherited_velocity = true + + current_target_velocity = $GroundCheckRay.get_collider().velocity + + +# Reset the current target velocity and other variables relevant for moving platforms. +func reset_moving_platform_velocity(): + current_target_velocity = Vector3.ZERO + has_inherited_velocity = false + + # Called every frame. 'delta' is the elapsed time since the previous frame. func _physics_process(delta): var move_velocity := Vector3.ZERO @@ -105,6 +127,8 @@ func _physics_process(delta): # Get acceleration caused by gravity var gravity_acceleration = get_gravity_acceleration() + # FIXME: Consider setting the gravity_acceleration to 0 if on ground + # Apply both acceleration types apply_acceleration((move_acceleration + gravity_acceleration) * delta) @@ -119,8 +143,14 @@ func _physics_process(delta): velocity += -gravity_acceleration.normalized() * e_section time_since_jump_start += delta + # Check for moving platforms and lerp towards that + if is_on_moving_platform(): + apply_moving_platform_velocity() + else: + reset_moving_platform_velocity() + # Apply movement to position - velocity = move_and_slide(velocity, -gravity_acceleration.normalized(), true) + velocity = move_and_slide(velocity) # Clamp the velocity just to be save velocity.x = clamp(velocity.x, -MAX_VEL, MAX_VEL)