From 596f4ddbc512bf898772cf3c7db30dd4f761b958 Mon Sep 17 00:00:00 2001 From: karl Date: Wed, 16 Jun 2021 20:41:26 +0200 Subject: [PATCH] Some cleanup in Player --- Player.gd | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/Player.gd b/Player.gd index f292671..19579bb 100644 --- a/Player.gd +++ b/Player.gd @@ -12,7 +12,6 @@ var drag = 0.05 # Jumping var jumping := false -var on_ground := false var time_since_jump_start := 0.0 var initial_jump_burst = 10.0 @@ -24,8 +23,7 @@ export(NodePath) var solar_system func _input(event): - if event.is_action_pressed("jump") and on_ground: - on_ground = false + if event.is_action_pressed("jump") and is_on_ground(): jumping = true time_since_jump_start = 0.0 elif event.is_action_released("jump"): @@ -45,6 +43,28 @@ func apply_acceleration(acceleration): velocity += acceleration +func get_gravity_acceleration(): + # 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) + else: + return get_node(solar_system).get_gravity_acceleration(transform.origin) + + +# Returns true if the player is currently (almost) on the ground. +# Surfaces with an angle of 45° or less are considered a ground. +func is_on_ground(): + if $GroundCheckRay.is_colliding(): + var normal = $GroundCheckRay.get_collision_normal() + + # An angle of >45° to the local up vector counts as grounded + if normal.dot(global_transform.basis.y) > 0.5: + return true + + return false + + # Called every frame. 'delta' is the elapsed time since the previous frame. func _physics_process(delta): var move_velocity := Vector3.ZERO @@ -63,11 +83,10 @@ func _physics_process(delta): # Make movement local move_acceleration = transform.basis * move_acceleration - # Jumping and Gravity - var gravity_acceleration = get_node(solar_system).get_gravity_acceleration(transform.origin) - if $GroundCheckRay.is_colliding(): - gravity_acceleration = get_node(solar_system).get_closest_gravity_acceleration(transform.origin) + # Get acceleration caused by gravity + var gravity_acceleration = get_gravity_acceleration() + # Apply both acceleration types apply_acceleration((move_acceleration + gravity_acceleration) * delta) # Handle jumping @@ -83,24 +102,6 @@ func _physics_process(delta): # Apply movement to position velocity = move_and_slide(velocity, -gravity_acceleration.normalized(), true) - on_ground = false - - if get_slide_count() > 0: - var collision = get_slide_collision(0) - - if collision.collider.is_in_group("MovingPlatform"): - # 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: - # 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 - # Clamp the velocity just to be save velocity.x = clamp(velocity.x, -MAX_VEL, MAX_VEL)