2D platformer momentum lost after dash

Godot Version

4.3

Question

Hello, I'm new to Godot and trying to learn basic 2d platformer movement mechanics. I am currently designing a dash, but I'm having problems with the momentum:
If I dash while not holding a movement direction, I keep all my momentum for a while until friction brings my speed down. However, if I dash while holding a movement direction, my momentum quickly reduces down to my movement speed.
I would prefer to keep my momentum, even while holding a movement direction, but I cannot figure out how to do that.

func handle_horizontal_movement(body: CharacterBody2D, direction: float) -> void:
	var velocity_change_speed: float = 0.0
	if body.is_on_floor():
		velocity_change_speed = ground_accel_speed if direction != 0 else ground_decel_speed
	else:
		velocity_change_speed = air_accel_speed if direction != 0 else air_decel_speed
	
	
	body.velocity.x = move_toward(body.velocity.x, direction * speed, velocity_change_speed)
func dash(body: CharacterBody2D, direction: float) -> void:
	
	if dash_cooldown_timer.is_stopped() and is_dashing == false:
		
		is_dashing = true
		
		get_tree().create_timer(dash_time).timeout.connect(_on_dash_finished_timeout)
		
		var DashTween = get_tree().create_tween().set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_SINE).set_process_mode(Tween.TWEEN_PROCESS_PHYSICS)
		
		DashTween.tween_property(body,"velocity:x", dash_speed * direction , dash_time)
	

	dash_cooldown_timer.start()
	

You can do like this:

if !is_dashing: body.velocity.x = move_toward(body.velocity.x, direction * speed, velocity_change_speed)

Make sure that you are making the is_dashing variable to false after the dash completed.

2 Likes

Thank you! this worked perfectly

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.