Welcome to the forums @David8991!
There are several ways to accomplish this, and it really depends on the type of game you are looking for and how you want to program it.
For creating a jetpack, it is just an extension of a jump but with a timer. A simple implementation of a jetpack could work like this (untested):
# You'll likely need to adjust these values
var jetpack_fuel = 2
const JETPACK_STRENGTH = 32
const JETPACK_MAX_FUEL = 2
func _physics_process(delta):
# whenever the jump/jetpack button is pressed:
if Input.is_action_pressed("Jump"):
if jetpack_fuel > 0:
velocity.y = JETPACK_STRENGTH
jetpack_fuel -= delta
# if you want to reset/refuel the jetpack:
# (for example, refuel it when on the ground, assuming the node is a KinematicBody2D)
# if is_on_floor() == true:
# jetpack_fuel = JETPACK_MAX_FUEL
For showing the progress, you can use a Range-based node (documentation), and just set the max_value
to the same value as JETPACK_MAX_FUEL
and the min_value
to 0
. Then you just need a quick bit of code to update the value.
For example, adding the UI to the code example above can be done like this (untested):
# You'll likely need to adjust these values
var jetpack_fuel = 2
const JETPACK_STRENGTH = 32
const JETPACK_MAX_FUEL = 2
# the UI node to use for showing the jetpack fuel.
var range_node = null
func _ready():
# You'll need to change this line so it gets the UI node in the scene that you want to use
range_node = get_node("ProgressBar")
# set the min, max, and value through code
range_node.min_value = 0
range_node.max_value = JETPACK_MAX_FUEL
range_node.value = jetpack_fuel
func _physics_process(delta):
# whenever the jump/jetpack button is pressed:
if Input.is_action_pressed("Jump"):
if jetpack_fuel > 0:
velocity.y = JETPACK_STRENGTH
jetpack_fuel -= delta
# if you want to reset/refuel the jetpack:
# (for example, refuel it when on the ground, assuming the node is a KinematicBody2D)
# if is_on_floor() == true:
# jetpack_fuel = JETPACK_MAX_FUEL
# update the UI
range_node.value = jetpack_fuel
Finally, for fuel collectables, you just need to have the collectable fuel increase jetpack_fuel
when the player collides/interacts with the fuel. You'll probably need to clamp it, so I would recommend making a quick helper function like the following:
func add_fuel(amount):
jetpack_fuel = clamp(jetpack_fuel, 0, JETPACK_MAX_FUEL)
Then you can do something like player.add_fuel(amount_to_add)
in your fuel pickup to add fuel to the player.
Of course, this is just one way to tackle the issue. There are other ways that would work as well, I just wrote the first method that came to mind.