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:
The
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.
Create a new scene and click on the 2D Scene button to start with a
root node. Rename the node to PushWall.We start with a
as we will add two child nodes that move independently.Save the scene in the ObstacleCourse/
directory.
Add a
node with a and child nodes.We use the
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
as a child of the will allow us to move it in sync with its parent and its collision shape.Select 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 up and align the pivot with its bottom
edge.
To make the wall block the player, select the Shape
property.
Click the newly created 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:
To trigger the push wall as the player approaches, we will use an
node and make its collision shape larger than the wall. The wall will start pushing as the player enters the area.Add an
node as a child of PushWall and create a new node as its child.In the Inspector, assign a new 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.