07.preventing-player-from-leaving-track

Preventing the player from leaving the track

In this short video, we draw collision polygons around the level to prevent the player from walking over the empty space.

We’ll add a start countdown to the game in the following lesson. That’ll be the first step towards completing our mini-game.

Preventing the player from getting stuck behind the push wall

The push wall that we designed in the previous lesson has an issue. You can trigger the wall, wait, and move behind it if you time your movement well.

When the wall moves up, you get pushed outside the level in the collision polygons we drew.

In this case, the player can thankfully free themselves from the wall. It could be worse.

It’s a common issue with game physics. If you play games often, you’ve certainly gotten physically stuck in a level and had to reload the game.

This happens when the collision shapes are not the right size, or there is an error in physics calculations. In our case, the wall collider is too thin. You shouldn’t be able to walk behind it in the first place.

The simplest solution to this problem is to make the PushWall’s collision shape taller.

Open the PushWall scene and select the CollisionShape2D under the StaticBody2D node.

Use the top resize handle to increase the shape size vertically. You can make it as tall as or taller than the wall to prevent the player from walking behind it.

With that, the player won’t get stuck behind the push wall anymore.