Attempt at making grounded movement work well

This commit is contained in:
karl 2021-06-07 19:02:24 +02:00
parent 5f17289426
commit 825bc032da

View File

@ -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,16 +85,20 @@ 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 get_slide_count() > 0:
var collision = get_slide_collision(0)
if collision:
if collision.collider.is_in_group("MovingPlatform"):
# Inherit the moving platform's velocity
current_target_velocity = collision.collider.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:
# 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