In the first obstacle course series, we used invisible walls to prevent the player from going off course.
In terms of gameplay, this doesn’t feel great. We should think of a way to improve that.
One option would be to have the character bounce against obstacles, but it would feel a bit off because the islands don’t have outer walls.
Instead, as many racing games do, we could slow down the player when going off course.
This change will play well with the speed boost pickup that we’ll add in the next lesson.
This kind of gameplay change happens a lot when creating games. You never know how players will understand or like a mechanic until they test it.
So you have to be ready to change the gameplay, the game’s controls, and the code a lot during development based on your players’ feedback.
That’s what we’ll do here. Thankfully, we can reuse our collision polygon and adjust the code in this case.
We’ll first convert the OffCourse node from a
into an so the player can walk outside of the islands again.Then, we’ll add code to the Godot.gd
script to change
the character’s speed when going in and out of the area.
Open the ObstacleCourse.tscn
scene in the
ObstacleCourse_Part2/
folder.
It contains a
node named OffCourse, which blocks the player completely. We need to convert it into an to detect when the player walks off course and not block them.Right-click on the OffCourse node in the Scene dock and select Change Type from the list. You can use this command to convert a node into a different node type.
Search for
and press the Change button to convert the node.In Godot, you can assign tags to nodes using a feature called node groups. You can later use them in place of collision layers to filter interactions in code.
Let’s assign our OffCourse node to a group to later distinguish going outside the track course from hitting a PushWall in our code.
Click the Node tab next to the Inspector, then click its Groups sub-tab to access the node groups dock.
In the field next to the Add button, type “offcourse” and
press Enter to assign the offcourse
tag to the
node.
A new icon appears next to the OffCourse node in the Scene dock.
Note: While groups can be more convenient than using collision layers, using them in a game with thousands of physics objects will affect your game’s performance. In that case, prefer collision layers. But for dozens and perhaps even hundreds of physics objects, it shouldn’t make much of a difference.
We can now update the character’s code to slow down when entering the OffCourse area.
Open the Godot.tscn
scene in the
ObstacleCourse_Part2/
directory, then click the script icon
next to the Godot node to open its script.
Until now, we only had one speed value for our character, but as we are adding the slow zone, we want to have two speed values: one for normal speed and one for when the character walks off course.
The character’s speed will not be constant anymore. Instead, we need a variable.
As we mentioned before, we use names in
CAPITAL_SNAKE_CASE
for constants but
lowercase_snake_case
for variables, so we want to rename
our speed constant accordingly. But we shouldn’t do this by hand.
We can quickly replace variables, functions, and anything else in code using the search and replace tool. To open it, in the script editor, press Ctrl+r.
You type the variable to rename in the top bar and the replacement in the bottom field.
Then, click Replace All to replace every match. Finally
replace the const
keyword with var
. You’ve
refactored (changed) a constant into a variable.
var speed := 700.0
We now have two speed values instead of one, so we introduce two constants to keep track of them. We will later add a third speed constant for the speed boost mechanic.
const SPEED_SLOW := 100.0
const SPEED_DEFAULT := 700.0
# We update the speed variable to use our new constant.
var speed := SPEED_DEFAULT
All that’s left is to slow down the character when entering the OffCourse area and to restore their speed when they leave the area.
Because we need to modify the character’s speed, we ideally want to keep that code on the character itself.
However, we currently don’t have a way to detect when the character goes off course. To detect the OffCourse area, we need an
node on the character.Open the Godot.tscn
scene in the
ObstacleCourse_Part2/
directory and add a new node with a as a child of Godot.
Give the
node a and make it roughly as big as Godot’s existing collision shape.Then, head back to Godot’s script, where we get the area
node, connect to its area_entered
and
area_exited
signals, and change the character’s speed in
the callback functions.
onready var slowdown_area := $Area2D
func _ready() -> void:
# ...
connect("area_entered", self, "_on_Area2D_area_entered")
slowdown_area.connect("area_exited", self, "_on_Area2D_area_exited")
slowdown_area.
func _on_Area2D_area_entered(area: Area2D) -> void:
# When entering or leaving the area, we ensure that it has the offcourse
# node group to distinguish it from the push wall's area.
if area.is_in_group("offcourse"):
= SPEED_SLOW
speed
func _on_Area2D_area_exited(area: Area2D) -> void:
if area.is_in_group("offcourse"):
= SPEED_DEFAULT speed
You can test the game and walk off the track to see the character slow down. As soon as you get back on track, the character goes back to its normal speed.