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 MAXFALLSPEED = 50

func physicsprocess(delta):
if Input.is
actionpressed("Right"):
velocity.x = SPEED
\$Sprite.play("walk")
\$Sprite.flip
h = false
elif Input.isactionpressed("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:

in Engine

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.

by (232 points)

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

extends KinematicBody2D

var candoublejump = 0
var velocity = Vector2(0,0)
const SPEED = 180
const GRAVITY = 30
const JUMPFORCE = -450
const MAX
FALLSPEED = 50
const RUN
SPEED = 290
const UP = Vector2(0,-1)

func physicsprocess(_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.