I am making a simple platformer to understand Godot better and I am trying to make a simple dash function (left and right) and I don't want to fall while dashing (I want to go perfectly horizontal during the dash) I am using GDscript and my setup is

---

  • KinematicBody2d
    • CollisionShape2d
    • Sprite

---

Here is my code as of now:

extends KinematicBody2D

export (int) var speed = 250 #walking speed
export (int) var jump_speed = -600 #the power of the jump
export (int) var gravity = 700 #the downwards force on the player
export (int) var max_fall_speed = 540 #the maximum speed the player can fall
export (int) var ground_pound_speed = 300 #how fast the player ground pounds
export (int) var dash_distance = 4000 #how far the dash takes the player

var direction
var velocity = Vector2.ZERO

func get_input():
	velocity.x = 0
	if Input.is_action_pressed("walk_right"):
		velocity.x += speed
		direction = 2 #right is equal to 2
	if Input.is_action_pressed("walk_left"):
		velocity.x -= speed
		direction = 1 #left is equal to 1
	if Input.is_action_just_pressed("dash"):
		if direction == 1:
			velocity.x -= dash_distance
		if direction == 2:
			velocity.x += dash_distance

func _physics_process(delta):
	get_input()
	velocity.y += gravity * delta
	velocity = move_and_slide(velocity, Vector2.UP)
	if Input.is_action_pressed("jump"):
		if is_on_floor():
			velocity.y = jump_speed
	if Input.is_action_just_pressed("down"):
		velocity.y = ground_pound_speed
	
func _process(delta):
	velocity.y = move_toward(velocity.y, max_fall_speed, gravity * delta)

write a condition under which you don't apply gravity to y component of velocity or just assign zero as the value for y. It kinda depends on what exactly you are trying to achieve.

edit: I see you already have conditions for dashing. Do it there I guess.

You could make your life a bit easier and create a is_dashing boolean/state. Then in the dashing input logic you toggle the state and then check that in the physics process.

As mentioned by @Megalomaniak, having a boolean flag to know when you're in a current state is a pretty common way to do this. You can set it to true when the player presses the dash button. Then in your _physics_process(delta) function, you can stop your y-axis movement by setting velocity.y = 0 * delta when if is_dashing == true. Then you can set it back to false when the distance has been traveled.

a year later