Double Jump Help

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

Hey so im new into this game making world and coding in general and im having a hard time implementing a double jump into my game. Been following a tutorial on youtube to make my game and here is the code i have so far:

extends KinematicBody2D

var velocity = Vector2(0,0)
const SPEED = 180
const GRAVITY = 30
const JUMPFORCE = -450
const MAX_FALL_SPEED = 50

func _physics_process(_delta):
if Input.is_action_pressed(“Right”):
velocity.x = SPEED
$Sprite.play(“walk”)
$Sprite.flip_h = false
elif Input.is_action_pressed(“Left”):
velocity.x = -SPEED
$Sprite.play(“walk”)
$Sprite.flip_h = true
else:
$Sprite.play(“idle”)

velocity.y = velocity.y + GRAVITY

if Input.is_action_just_pressed("Jump") and is_on_floor():
	velocity.y = JUMPFORCE

velocity = move_and_slide(velocity,Vector2.UP)

velocity.x = lerp(velocity.x,0,0.2)

Im gratefull for any help i can get and thanks for the time.

PS: tutorial series im watching:

:bust_in_silhouette: Reply From: Schweini

if !is_on_floor() and can_doublejump and Input is_action_just_pressed("Jump"): velocity.y = jump_force can_doublejump = false

With this snippet if you press jump in the air and the variable can_doublejump is true, the player will jump. You should set can_doublejump false before the player jumped the first time. And of course you have to define this variable before all of this.

Hey thanks for the help but im having a hard time implementing that to the code.

extends KinematicBody2D

var can_doublejump = 0
var velocity = Vector2(0,0)
const SPEED = 180
const GRAVITY = 30
const JUMPFORCE = -450
const MAX_FALL_SPEED = 50
const RUN_SPEED = 290
const UP = Vector2(0,-1)

func _physics_process(_delta):

if Input.is_action_pressed("Run"):
	if velocity.x > 0: 
		velocity.x = velocity.x + 60
		$Sprite.play("walk")
		$Sprite.flip_h = false
		if Input.is_action_pressed("Left"):
			if Input.is_action_pressed("Right"):
				velocity.x = 0
			else: 
				velocity.x = -velocity.x
	if velocity.x < 0:
		velocity.x = velocity.x - 60
		$Sprite.play("walk")
		$Sprite.flip_h = true
		if Input.is_action_pressed("Right"):
			if Input.is_action_pressed("Left"):
				velocity.x = 0
			else: 
				velocity.x = -velocity.x
	

elif Input.is_action_pressed("Right"):
	velocity.x = SPEED
	$Sprite.play("walk")
	$Sprite.flip_h = false
elif Input.is_action_pressed("Left"): 
	velocity.x = -SPEED
	$Sprite.play("walk")
	$Sprite.flip_h = true
	
else:
	$Sprite.play("idle")



velocity.y = velocity.y + GRAVITY

if Input.is_action_just_pressed("Jump") and is_on_floor():
	velocity.y = JUMPFORCE
	can_doublejump = false
	if !is_on_floor() and can_doublejump and Input.is_action_just_pressed("Jump"):
		velocity.y = JUMPFORCE can_doublejump = true
		

velocity = move_and_slide(velocity,Vector2.UP)

velocity.x = lerp(velocity.x,0,0.2)

I tried to implement it that way. if you could tell me what I did wrong I would be gratefull.

and thanks for the patience I have zero knowledge about programing.

skulrock | 2020-07-12 21:13

Firstly change var can_doublejump to var can_doublejump = false.

When the player jumps the first time can_doublejump should be set to true instead of false.

Also, you should should be on the left:

if !is_on_floor() and can_doublejump and Input.is_action_just_pressed("Jump"):
        velocity.y = JUMPFORCE 
        can_doublejump = false

Also note that I changed can_doublejump to true.

Good luck!

Schweini | 2020-07-13 19:26

Thanks a bunch. It worked after about 30 minutes messing with the code hehe

Again thanks for the patience for helping me out.

skulrock | 2020-07-13 21:00

What did you do to get the code to work, I’m really new to coding (YouTube for everything) i copied down everything that I was told to
It says Expected End of Statement after expression, got identified instead, what do I do to fix it?

Panda Gamer | 2022-11-24 19:26

Can you post the code? Then I could identify the problem beter.

Schweini | 2022-11-24 20:05