Moving KinematicBody2D along slope without slowing horizontal speed

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lucaslcode

I’m making an auto-scrolling platformer, with the camera following a player moving at a constant horizontal speed.

I’m using move_and_slide to move and also go up and down slopes, however this slightly reduces the horizontal speed. Not enough to notice, but it will be a music-based game, so the cumulative effect would put it out of sync.

What’s the best way around this? I’ve tried increasing the max_slide_count, but it doesn’t make a difference. I even tried using move and collide to simply move the player, then try and adjust vertically as much as much necessary using a ray cast, but my code is obviously no good - the player gets stuck and doesn’t move. This seems over-complicated to me anyway, I’m sure there’s a better way.

move_and_collide code just in case:

last_collision = move_and_collide(delta * linear_vel, true, true, true)
var offset = Vector2(0, 0)
if last_collision:
	var space_state = get_world_2d().direct_space_state
	var attempt = global_position + delta*linear_vel
	var collision_shape = Physics2DShapeQueryParameters.new()
	if last_collision.normal.y > 0:
		var collision = space_state.intersect_ray(attempt - $Edge.position + RAY_LENGTH, attempt - $Edge.position, [], collision_mask & (2+4+32))
		offset = collision.position - (attempt - $Edge.position)
	elif last_collision.normal.y < 0:
		var collision = space_state.intersect_ray(attempt + $Edge.position - RAY_LENGTH, attempt + $Edge.position, [], collision_mask & (2+4+32))
		offset = collision.position - (attempt - $Edge.position)
	else:
		die()
	position += delta*linear_vel + offset

I don’t have any experience implementing this, but there’s a bit of information about it in this post: Godot 3.1 will get many improvements to KinematicBody

denxi | 2020-02-18 14:13