I want to make my player glide once it's in the air, but I don't know how.

Godot Version

Godot 3.6

Question

How do I make the player glide?

I’ve looked everywhere I can, but nothing I’ve seen explains how to make the player glide, or the search results were unrelated.
I just want to make it so that when the player is in the air after jumping, when the player presses the jump button, the player begins moving more slowly towards the ground. I’ve tried changing the gravity and variables, but that didn’t work.
I’m fairly new to GDScript, but I understand the basics like player movement, variables, functions, things like that. But I can’t get my player to glide.
Sorry if I’ve done something wrong in terms of posting, this is my first time posting on here :slight_smile:

my player’s current code:

const SPEED = 150
var GRAVITY = 12
const JUMP_POWER = -350
const FLOOR = Vector2(0, -1)

var velocity = Vector2()
var on_ground = false

func _physics_process(_delta):
	if Input.is_action_pressed("ui_right"):
		velocity.x = SPEED
	elif Input.is_action_pressed("ui_left"):
		velocity.x = -SPEED
	else:
		velocity.x = 0
	
	if Input.is_action_pressed("ui_up"):
		if on_ground == true:
			velocity.y = JUMP_POWER
			on_ground = false
	
	velocity.y += GRAVITY
	
	if is_on_floor():
		on_ground = true
	else:
		on_ground = false
	
	move_and_slide(velocity, FLOOR)

In the Godot editor, go to “Project->Project Settings->Input Map”. In the field “new action”, enter the name glide and press “add”. A new option appears. Now click on the little pencil on the right next to the option. You now get a new screen where you can bind a keyboard button to the new action “glide” that you just defined. (It doesn’t even have to be a keyboard key, it can even be gamepad input or a mouse click.) Now click ‘close’ twice.

You now have a button that, if you press it, your game knows that you want to glide. The code can be edited to account for this new feature:

const SPEED = 150
var GRAVITY = 12
const JUMP_POWER = -350
const FLOOR = Vector2(0, -1)

var velocity = Vector2()
var on_ground = false
var is_gliding : bool = false
var glide_factor : float = 0.2

func _physics_process(_delta):
	if Input.is_action_pressed("glide"):
		is_gliding = true
	else:
		is_gliding = false

	if Input.is_action_pressed("ui_right"):
		velocity.x = SPEED
	elif Input.is_action_pressed("ui_left"):
		velocity.x = -SPEED
	else:
		velocity.x = 0
	
	if Input.is_action_pressed("ui_up"):
		if on_ground == true:
			velocity.y = JUMP_POWER
			on_ground = false
	
	if not is_gliding:
		velocity.y += GRAVITY
	else:
		velocity.y += (glide_factor * GRAVITY)
	
	if is_on_floor():
		on_ground = true
	else:
		on_ground = false
	
	move_and_slide(velocity, FLOOR)

Is the ‘glide’ button pressed? If so, we set is_gliding to true. If it’s not pressed, we set is_gliding to false. Later on in the code we check if that bool is set. If not, we’re falling like we normally would. If it is set, then we multiply the gravity with a value smaller than one. This causes the player to fall slower than normal.

There are quite a few caveats with the code. Not least of which is that you can glide in situations where you might not expect it. For instance, if instead of jumping you step off of a cliff, you can still glide. But this example is not meant to be comprehensive; it’s just something to get you started.

I hope this is useful.

Your glide will need to fight against gravity every frame.

So if player is in air and glide button pressed add glide force (-value) to velocity.y. just make it less then gravity and it should work.

Thank you so much, this really helps! :))