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!")
