04.push-wall-obstacles

Creating the pushing wall scene

In the next three lessons, we’ll create a wall that pushes the player off the track.

Once we add invisible walls around the track to keep the player from cutting corners, the push wall obstacle will stop the player’s movement by trapping them against invisible walls.

We’ll need three things:

  1. A wall that collides with the player.
  2. A push animation.
  3. A way to detect when the player walks in front of the wall to trigger the animation.

The Area2D node detects collision nodes overlapping, entering, or exiting it. We’ll use it to trigger the wall’s animation.

Let’s start by setting up the wall’s scene.

Creating the scene

Create a new scene and click on the 2D Scene button to start with a Node2D root node. Rename the node to PushWall.

We start with a Node2D as we will add two child nodes that move independently.

Save the scene in the ObstacleCourse/ directory.

Add a StaticBody2D node with a Sprite and CollisionShape2D child nodes.

We use the StaticBody2D node to block the player. Static bodies only represent obstacles. While the name implies that they can not be moved, you can move them. They just don’t do much more than block other bodies.

Having the Sprite as a child of the StaticBody2D will allow us to move it in sync with its parent and its collision shape.

Select the Sprite node, right-click on the Texture slot in the Inspector, and select Quick Load. Load the wall.png image into it.

Also, set the Offset -> Offset y-value to -64 to shift the Sprite up and align the pivot with its bottom edge.

To make the wall block the player, select the CollisionShape2D node. In the Inspector, assign a RectangleShape2D resource to the Shape property.

Click the newly created RectangleShape2D to edit it and set its Extents to Vector2(64, 16).

The Extents are half the size of the collision shape. A value of Vector2(64, 16) means that the rectangle will be 128 pixels long and 32 pixels tall.

We want to align the bottom of the rectangle shape with the bottom of the wall, so the collision area closely matches the wall.

Set the Transform -> Position y-value to -16 to move the shape up by 16 pixels.

This is how your scene should look:

Creating the player detection area

To trigger the push wall as the player approaches, we will use an Area2D node and make its collision shape larger than the wall. The wall will start pushing as the player enters the area.

Add an Area2D node as a child of PushWall and create a new CollisionShape2D node as its child.

In the Inspector, assign a new RectangleShape2D to the Shape property. This time, set its Extents to Vector2(128, 64) to make it wider than the wall.

Your scene should look like this.

With that, our wall is ready to detect the player walking over it. However, we lack the push animation to code and test our new obstacle.

We’ll design the animation in the next lesson.