Extra: Boost
This content is not available in your language yet.
Boost
Section titled BoostIf you want a little extra help with speed, here we go!
-
In the
hud.tscn
scene: Add a ColorRect node under the main Control node.- Rename it to
Boost
and change it’s size to an upright rectangle on the right side of the screen. - Change the colour to transparent(ish) dark green.
- Add another ColorRect node under the main ColorRect
Boost
node. Rename this toBoostIndicator
. - Add
BoostIndicator
to thelabels
global group (in Node > Signals). - Add a Label as a child of the
Boost
node and set the text toBoost
. Set the scale to 1.5.
- Rename it to
-
Bind a ‘Boost’ to your Space Bar in the Settings > Input Map.
-
Inside our
car.gd
, add in these changes.car.gd var boost_multiplier = 1.5 # Factor to increase acceleration during boostvar boost_duration = 2.0 # Boost lasts for 2 secondsvar boost_cooldown = 5.0 # 5 seconds cooldown between boostsvar is_boosting = false # Tracks if boost is activevar boost_timer = 0.0 # Timer to track boost durationvar cooldown_timer = 0.0 # Timer for boost cooldownvar boost_rect_size = 370 # Change this to your rectangles current size!var boost_indicatorfunc _ready() -> void:# (exisiting code)boost_indicator = labels.filter(func(label): return label.name == "BoostIndicator")[0]func _process(delta: float) -> void:# Handle boost activation with space barif Input.is_action_just_pressed("Boost") and cooldown_timer <= 0.0:start_boost()# Update timers if boosting or in cooldownif is_boosting:boost_timer -= delta# the lerp function maps the timer to the boost bar.boost_indicator.size.y = lerp(0, boost_rect_size, boost_timer / 2.0)if boost_timer <= 0.0:end_boost() # End boost when timer runs outelif cooldown_timer > 0.0:cooldown_timer -= deltaboost_indicator.size.y = lerp(boost_rect_size, 0, cooldown_timer / 5.0)boost_indicator.color = Color('ca7566a5') # transparent redelse:boost_indicator.color = Color('caff66a5') # transparent greenfunc _physics_process(delta: float):# steering & speed physics# (exisiting code)var acceleration = Input.get_axis("Backward", "Forward")# Add a check if the car is boosting between acceleration declaration# and before the rpm declarationif is_boosting:acceleration *= boost_multiplier # Apply boost multipliervar rpm = abs($BackLeftWheel.get_rpm())# (exisiting code)func start_boost():is_boosting = true # Activate boostingboost_timer = boost_duration # Set the boost duration timercooldown_timer = boost_cooldown # Set the cooldown timerfunc end_boost():is_boosting = false # Deactivate boosting
You should now have something like this:
When pressing space bar:
When the boost is reloading: