#Issue getting double jump logic to work as intended

1 messages · Page 1 of 1 (latest)

modest cove
#

I'm trying to create a system where the player has an energy bar that refills over time. I want give the player to do a normal jump for no cost, and then if they jump again mid-air, it will take away 50 energy to perform a double jump. I'm having an issue where both the normal jump and double jump happen at the same time even though the double jump shouldn't be firing. Here's the script:

extends CharacterBody2D

### Variable Stats ###
@export var health = 100
@export var energy = 100
var is_grounded = false
var has_double_jumped = false

### Constants ###
const SPEED = 450.0
const JUMP_VELOCITY = -600.0
const DOUBLE_JUMP_COST = 50

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")


func _process(_delta):

    if velocity.x <= -1:
    # $Sprite.flip_h THIS SHOULD FLIP THE SPRITE IDK WHY IT CRASHES AAAAAAAAAA
        pass

# Handle physics processing
func _physics_process(delta):
    ### Regenerate energy over time ###
    if energy < 100:
        energy += 5 * delta  # Gradual energy regeneration
    else:
        energy = 100

    ### Apply gravity if not on the ground ###
    if not is_on_floor():
        velocity.y += gravity * delta

    ### Horizontal movement based on input ###
    var direction = Input.get_axis("ui_left", "ui_right")
    if direction:
        velocity.x = direction * SPEED
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)
        
    ### Jumping Logic ###
    if Input.is_action_just_pressed("game_jump"):
        if is_on_floor():
            do_jump()
            has_double_jumped = false
            # does a regular jump if jump key is pressed on the ground
        elif is_grounded == false and energy >= (DOUBLE_JUMP_COST + 1):
                do_double_jump()
                # does a double jump is jump key is pressed in the air
        
        # Debugging purposes
        if energy < DOUBLE_JUMP_COST:
            print("Not enough energy!")

    move_and_slide()
    
    is_grounded = is_on_floor()

### Jump functions ###
func do_jump():
    velocity.y = JUMP_VELOCITY
    print("Jumped!")
    
func do_double_jump():
    energy -= DOUBLE_JUMP_COST
    velocity.y = JUMP_VELOCITY
    has_double_jumped = true
    print("Double Jumped!")
#

i've been trying to solve this for so long, i should add i did ask chatgpt to see if it could help me solve it, and it helped me solve one issue where i had the jump input firing every frame (bc im stupid and wasn't using is_action_just_pressed), but the code is basically all my mess aside from that lmao

proven adder
#

For both jumps, can you change the velocity part to this:
velocity.y -= JUMP_VELOCITY (you're missing a minus)

#

Or alternatively:
velocity.y = -JUMP_VELOCITY

#

Ah wait, sorry... I see that your jump_vel is already negative

modest cove
#

i mean it still DOES the jump fine, the issue is just that does both a jump and a double jump when jumping off the groung

proven adder
#

usually people have positive values for that, and then use negatives in the math

modest cove
#

so it essentially consumes the 50 energy even though really the player should've just done a normal jump

proven adder
#

because your elif uses is_grounded instead of is_on_floor()

#

change the elif:
elif not is_on_floor() and energy...

modest cove
#

no difference im afraid (is_grounded was a variable i was using for smth and idr why at this point lol)

proven adder
#

But this makes no sense... because you have an if and elif that is mutually exclusive:
if is_on_floor()
elif not is_on_floor()

They cannot be triggered at the same time.

Could it be that your "pressing the jump button" gets triggered twice because your keyboard has a high repeat or something?

#

Wait.. it's because of the just pressed.

#

can you change the button part? remove just

#

so: is_pressed()

modest cove
#

isnt the just pressed supposed to fix the whole issue w/ it activating multiple times?

#

i mean i can try?

proven adder
#

because physics runs at least like 30 if not 60 fps

#

so you pressing a button is much slower, and therefor, gets triggered

modest cove
#

yeah removing the just makes it just activate even more, should i maybe move it to normal process instead of physics then?

proven adder
#

might help

modest cove
#

man im starting to think im just cursed or smth, nothing seems to make a difference

#

obvs that's not the case, i've just done smth stupid, but idk wtf it is i've done

tacit plank
#

I've just had a look at the video, and there's something a bit odd going on. If you look at the order of the events, it seems to be double jumping first, and then regular jumping?

proven adder
tacit plank
#

And it shouldn't even be getting past the initial if statement. if is_on_floor() should be true.

tacit plank
#

Maybe it would help adding print("New frame") at the top of the physics process, just to double check what frame everything is happening on?

#

"Not enough energy" is also printing before "jumped". It's like it's doing the script in reverse, almost.

proven adder
#

That's cursed alright

tacit plank
proven adder
#

I think the curse caught him...

tacit plank
#

Yeah. I do have a theory about what's going on though. I have a theory that the script is fine, and it's another one that's messing with things.

proven adder
modest cove
modest cove
#

ok honestly there's barely any scripts running. There's just that main one above and these 3:

#

would it be an issue that this is a thing?

#

Using it so the ui can access info for displaying current energy & stuff

modest cove
#

i swear the more i look into this the more confused i become

proven adder
modest cove
#

it's the entire script for the character, since that's where the energy variable & such is. aka it's the script i sent at the start

#

oop hold on i might've fixed it??

#

i think it was an issue w/ it being global

#

I've disabled the global, changed the energy bar's code to this, and now it seems to work?

#

so glad i've finally sorted that, thank you for the help either way ❤️

tacit plank
#

No problem. If you did want to keep a global reference to the player's energy, you can store the energy itself in an autoload, and just have the player reference it. That way, you don't have to use $../../.. to get at it.