0 votes

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:
https://www.youtube.com/watch?v=dC6G7Y9qRbQ

in Engine by (12 points)

1 Answer

0 votes

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.

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!

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

Again thanks for the patience for helping me out.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.