2D Platformer How to Solve Horizontal Clamp Limiting Dash Function

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

Hi, I’m very new to Godot (2 days old) and I wanted to make a simple 2D platformer. I followed some tutorial on youtube and ended up having this code as my movement controller.

https://pastebin.com/raw/KBRNMJK9

Here’s a snippet of where my problem lies:

		func _physics_process(delta):
var x_input = Input.get_action_strength("right") - Input.get_action_strength("left")
if x_input != 0:
	motion.x += x_input * acceleration * delta
	motion.x = clamp(motion.x, -maxspeed, maxspeed)
		
	sprite.flip_h = x_input < 0
	if is_on_floor() and motion.x !=0:
		animationPlayer.play("Run")
	if not is_on_floor() or is_on_wall() or is_on_ceiling():
		motion.x = lerp(motion.x, 0, airfriction)
		
else:
	if is_on_floor():
		motion.x = lerp(motion.x, 0, friction)
		animationPlayer.play("Stand")
	if not is_on_floor() or is_on_ceiling():
		motion.x = lerp(motion.x, 0, airfriction)

motion.y += gravity * delta

if is_on_floor():
	jumpsleft = 3
	availjumps = 24
	if Input.is_action_just_pressed("jump"):
		isjumping = 1
		animationPlayer.play("Jump")
		motion.y = -jumpforce
		jumpsleft = jumpsleft - 1
		
	if Input.is_action_just_pressed("Dodge") and availdodge > 0:
		print("Dodge")
		motion.x = x_input * 2000
		availdodge = 0
		availdodge = 1

As you can see I clamped the motion.x to a maxspeed to stop the player from accelerating too much. But this also blocks my dashing (i wrote it dodge in the script). Basically what happens is that when i press dodge/dash i only teleports a few blocks forward and not dash like the way i imagined it would be. Which is more or less like dashing in any other platformer games. I tried raising the clamp whenever i press dodge but it results in me being unable to revert to the old maxspeed, and also i think it’s not a really good solution to this?

Edit: If i remove the clamp i can dash just like i want but i keep on accelerating ):

That’s about it. Thanks for reading, i hope i wrote it clear enough.