Fix moving platform behavior

It seems like when you pass an up_vector to move_and_slide, that handles moving platforms for you! Although not 100% correctly...

The manual implementation was added again without an up_vector and that seems to work perfectly now.
This commit is contained in:
karl 2021-06-19 01:11:16 +02:00
parent 62956d8d7d
commit 0f90dd61f2

View File

@ -20,6 +20,7 @@ export var jump_exponent = 0.05
export var almost_on_ground_length = 1.0 export var almost_on_ground_length = 1.0
var current_target_velocity := Vector3.ZERO var current_target_velocity := Vector3.ZERO
var has_inherited_velocity := false
export(NodePath) var solar_system export(NodePath) var solar_system
@ -84,6 +85,27 @@ func is_on_ground():
return false 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. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta): func _physics_process(delta):
var move_velocity := Vector3.ZERO var move_velocity := Vector3.ZERO
@ -105,6 +127,8 @@ func _physics_process(delta):
# Get acceleration caused by gravity # Get acceleration caused by gravity
var gravity_acceleration = get_gravity_acceleration() var gravity_acceleration = get_gravity_acceleration()
# FIXME: Consider setting the gravity_acceleration to 0 if on ground
# Apply both acceleration types # Apply both acceleration types
apply_acceleration((move_acceleration + gravity_acceleration) * delta) apply_acceleration((move_acceleration + gravity_acceleration) * delta)
@ -119,8 +143,14 @@ func _physics_process(delta):
velocity += -gravity_acceleration.normalized() * e_section velocity += -gravity_acceleration.normalized() * e_section
time_since_jump_start += delta 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 # 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 # Clamp the velocity just to be save
velocity.x = clamp(velocity.x, -MAX_VEL, MAX_VEL) velocity.x = clamp(velocity.x, -MAX_VEL, MAX_VEL)