In this lesson, we’ll turn our rock sprites into obstacles that’ll slow down the player in their course.
Randomness plays an important role in games. Placing obstacles that change on every play forces the player to adapt a bit and makes the game more replayable.
The replayability will be minimal in this project, but we’ll later build a more complex project that has more variable elements.
Like before, you’ll find the rock scenes in the
ObstacleCourse/obstacles/rocks/
directory. Each scene
(Rock1.tscn
, Rock2.tscn
, and
Rock3.tscn
) contains one node. We’re going to add a collider to each
of them to turn them into obstacles for the player.
We need to add a
to each rock. The node turns a scene into an obstacle. You’ll use it for walls, doors, rocks, etc.We’re going to create a new scene and instantiate it in each rock scene, so all three rocks share the same collision shape.
Create a new scene and select Other Node from the Scene dock.
Create a
node. You can type “staticbody2d” in the Create New Node window to find it quickly.The
node needs a child collision node so that Godot can calculate physics interactions.In games, we usually separate visuals from physics interactions. We have two types of collision nodes for 2D games:
and .We’ll use
nodes to create round shapes along which the character will slide a bit. Add a node to the scene.Then, add a Shape
slot in the
Inspector.
Expand the resource properties by clicking on the
name in the Inspector.You can click the
you just created to expand it in the Inspector and change its properties.Set the capsule’s Radius to 20
and its
Height to 10
. This makes it large enough to fit
the rock obstacles.
Note that the capsule doesn’t have to fit the rock texture to create the desired effect.
The
resource gets longer vertically depending on the Height value.Because our rocks have more width than height, we should rotate the
90
degrees.
Set the Transform -> Rotation Degrees to
90
.
Your scene should now look like this.
Save the scene in the ObstacleCourse/obstacles/rocks/
directory.
Open the three rock scenes in the
ObstacleCourse/obstacles/rocks/
directory. We want to add
an instance of our StaticBody2D.tscn
scene to each of
them.
After opening Rock1.tscn
, press
Ctrl+Shift+A to open the Instance
Child Scene popup. This window lets you search and instantiate any
scene in your project.
Search for “obstaclecoursestaticbody2d” to find your scene and press Enter to instantiate it.
You should have the following scene tree.
We also need to reposition the
to overlap the underlying sprite roughly. With the Select tool active, click and drag the capsule in the viewport until it covers the rock’s base. That’s the part against which the player will collide and stop.Here’s how my Rock1 scene looks.
You’ll want to repeat these steps for Rock2.tscn
and
Rock3.tscn
.
We can test that the rocks now block the player in a temporary scene.
Right-click on the ObstacleCourse/
directory and click
New Scene.
You can leave the default name and click the 2D Scene button in the scene dock to create a
.We’re going to instantiate our three rocks and our character all at
once. In the FileSystem dock, expand the
ObstacleCourse/
directory and its
obstacles/rocks/
subdirectory. Click on
Rock1.tscn
, then keep the Ctrl key down and
click on Rock2.tscn
, Rock3.tscn
, and
Godot.tscn
to add them to your selection.
Click and drag any of the selected files into the viewport. It will add all four to the scene at once.
You’ll need to move the rocks away from the character to have some space to walk and collide with them.
Press F6 to run the scene and use the WASD keys to move against the rocks. The player should collide with them and slide against the capsule shapes.
If you can’t see the collision shapes, quit the running scene by pressing F8, then go to the Debug menu and turn on Visible Collision Shapes. Then, rerun the scene.
We ensured collisions worked as intended. Now, we can delete the
temporary scene. Right-click on the new scene.tscn
file in
the FileSystem dock and select Delete.
Open the practice The maze.
In this practice, you need to add collision shapes to three scenes: Godot, RobotStatue, and BrokenWall.
All three scenes should use a
node with a rectangle collision shape.The broken wall’s shape should entirely cover the sprite so the player can’t walk out of the level.
The statue’s collision shape should cover the base on which it lies. That’ll allow the character to walk behind the statue.
As for the character, any small shape will do.
Note: You can scale the shape symetrically by pressing Alt as you drag one of the handles.
In the next lesson, we’ll start creating a second obstacle for our game: the pushing wall trap.