diff --git a/Player.gd b/Player.gd index 19579bb..0d7c49d 100644 --- a/Player.gd +++ b/Player.gd @@ -3,25 +3,31 @@ extends KinematicBody const MAX_VEL = 500.0 -var acceleration := Vector3(0.0, -9.81, 0.0) -var velocity := Vector3(0.0, 0.0, 0.0) +var acceleration := Vector3.ZERO +var velocity := Vector3.ZERO -var move_accel = 60.0 -var rotate_speed = 2.0 -var drag = 0.05 +export var move_accel = 60.0 +export var rotate_speed = 2.0 +export var drag = 0.05 # Jumping var jumping := false var time_since_jump_start := 0.0 -var initial_jump_burst = 10.0 -var jump_exponent = 0.05 +export var initial_jump_burst = 10.0 +export var jump_exponent = 0.05 + +export var almost_on_ground_length = 1.0 var current_target_velocity := Vector3.ZERO export(NodePath) var solar_system +func _ready(): + $GroundCheckRay.cast_to = Vector3.DOWN * almost_on_ground_length + + func _input(event): if event.is_action_pressed("jump") and is_on_ground(): jumping = true @@ -44,12 +50,25 @@ func apply_acceleration(acceleration): func get_gravity_acceleration(): + var total_gravity = get_node(solar_system).get_gravity_acceleration(transform.origin) # If we're (almost) on the ground, accelerate only towards the planet we're on the ground of. # Otherwise, get the total gravity of the solar system. if $GroundCheckRay.is_colliding(): - return get_node(solar_system).get_closest_gravity_acceleration(transform.origin) + # Lerp between the planet's own gravity and the + var planet_gravity = get_node(solar_system).get_closest_gravity_acceleration(transform.origin) + + var distance_to_collision = ($GroundCheckRay.get_collision_point() + - $GroundCheckRay.global_transform.origin).length() + + # This factor is 0.0 if the player is exactly on the ground, and 1.0 if they're just barely + # almost grounded + var factor = inverse_lerp(0.0, almost_on_ground_length, distance_to_collision) + + return lerp(planet_gravity, total_gravity, factor) else: - return get_node(solar_system).get_gravity_acceleration(transform.origin) + # If the player is not grounded: return the whole acceleration caused by the entire solar + # system. + return total_gravity # Returns true if the player is currently (almost) on the ground.