How to Double Jump in Godot 4
Vložit
- čas přidán 22. 08. 2024
- So you've got a platform controller and now you want to be able to double jump?
Watch this video to see how.
You might need to integrate it with your other code but it's much easier than it seems if you understand what is going on.
Enjoy!
As some one who is learning how to code, its so hilarious how easy it is to double jump after seeing this video. I think the biggest help this video gave me was that I should start thinking outside the box and have that Programmer mentality that I am clearly missing. Thanks a lot mate!
PS: Just made a small improvement to the code so you are not allowed to double jump if you drop out of a ledge (it will only allow you to jump one time)
#Single jump if you drop off a ledge
if Input.is_action_just_pressed("jump") and not is_on_floor() and jump_count < max_jumps:
velocity.y = JUMP_VELOCITY
jump_count += 2
#Double jump.
if Input.is_action_just_pressed("jump") and jump_count < max_jumps:
velocity.y = JUMP_VELOCITY
jump_count += 1
if is_on_floor():
jump_count = 0
also put velocity.y = 0 at the top for double jump so that it resets your falling speed
And here I was racking my brain with bool variables and how to properly set them and creating for loops to constantly check and in the end its this simple? I really have a lot to learn still...
Thanks for this humbling but teaching experience!
Seriously- thank you SO much for this tutorial!!! I just started my first platformer last week and this was the only tutorial that helped me with this function! Thank you!!!
That is a lot simpler than what I was doing with a bool. Thanks
No worries
Works perfectly with 3d games too!
Yes it does.
Awesome tutorial!!
wow ive been trying to figure this one out and I was really close! haha! Thanks! It was a lot simpler than i thought.
Hi !
Nice tuto but I don't know why I can have infinite double jump... so there is my code (i have a dash too) :
extends CharacterBody2D
@export var speed = 300
@export var gravity = 30
@export var jump_force = 800
@export var dash_speed = 900
var dashing = false
var can_dash = true
var jump_count = 0
var max_jumps = 2
func _physics_process(delta):
if !is_on_floor():
jump_count = 0
velocity.y += gravity
if velocity.y > 1000:
velocity.y = 1000
if Input.is_action_just_pressed("jump"):
if jump_count < max_jumps:
velocity.y = -jump_force
jump_count += 1
var horizontal_direction = Input.get_axis("move_left","move_right")
velocity.x = speed * horizontal_direction
if horizontal_direction:
if dashing:
velocity.x = dash_speed * horizontal_direction
if Input.is_action_just_pressed("dash") and can_dash:
dashing = true
can_dash = false
$dash_timer.start()
$dash_again_timer.start()
move_and_slide()
func _on_dash_timer_timeout():
dashing = false
func _on_dash_again_timer_timeout():
can_dash = true
@mathislalonde353 make sure you've connected the tier to the jump timer timeout function
For 3d got to add something for when they player steps off a ledge so they only have 1 jump being their double jump hmmmm
Very helpful
thanks it worked.
Thanks this was very helpful 👍
worked for me, thank you
thanks so much!!!
this is the exact thing i did in javascript but it didnt work perfectly, sometimes it jump, sometimes did not.
How can I add my animation to it?
The only problem with this is that falling off of a ledge allows you to jump twice.
Yes but it's not bad for a five minute tutorial aimed at beginners! Have fun tweaking it to try to fix your issue. That's part of the fun!!
OMG. Thanks you so much❤
legend!
W tutorial
thank you for this. is there a way to change it in an area 2d?
In my project I want my player to double jump outside of water, but I have a block where I want jumping to be infinite but only inside it. I've gotten it to change gravity inside it, but it still treats the player on the floor so the jumps are still limited. Can you help me?
BRO HOW TO STRONG DOUBLE JUMP BRO???
What do you mean? The jump force is just a variable that you can change. And because you are "counting" the jumps with a different variable, you could change the jump force however or whenever you want.
@@SRCoder BRO I am a beginner
@@scarvvv but what do you mean by a "strong" double jump?
BRO I think YOU need to go back and review the basics
if jump_count >= 2:
velocity.y = JUMP_VELOCITY - 300
2:37
how could i make a double jump animation
nvm, did it
if jump_count == 2:
new_animation = &"DoubleJump"
Isn't programming easy!
whenever i jump twice i cant jump any more do you know what i did wrong
Did you watch the whole video? 😉
@@SRCoder yes
@@iliveunderarock8070 3:28
Incrível!
Thnks for avor
pleas C# cod😫😫😫😫😫😫😫😫
Why? The Beaty of godot is that its simple. Why make it harder by using c#?
@@SRCoder It tends to perform better and makes porting from Unity easier. I don't think you should feel pressed to make a tutorial for C# if you don't want to tho