CODING ACADEMY

View Original

How to Push a Block in Godot

Do you need to know how to push a block in Godot? In this tutorial, I will show you how you can easily get your platformer character to push a block or crate (or whatever you want!) in Godot.

If you already know how to create your character then you can skip ahead to STEP 4. Otherwise, follow along with me as we code everything from scratch.

WANT TO JOIN THE COURSE?

Join the course and gain access to resources to help you build the level shown in the video.

STEP 1: Create your Godot game scene

The first thing to do is create a new project. I will call mine “Push Block Demo”.

STEP 2: Create a new 2D scene

Create a new 2D scene by clicking on the 2D Scene button.

It is a good idea to get into the habit of renaming your nodes after you have added one. I will name my scene to ‘World’.

STEP 3: Adding a Player - CharacterBody2D - node to your scene.

  • Add a new CharacterBody2D child node to the scene.

  • Rename it to Player.

  • Add a Sprite2D child node to the Player.

  • Add a CollisionShape2D child node to the Player.

  • Add the default icon.svg sprite as the Sprite2D texture.

  • Add a RectangleShape2D as the collision shape and resize it to cover the Player character.

  • Group (icon to the right of the padlock) the 3 nodes within Player and move the Player into the game area.

STEP 4: Add a ground / floor

  • Add a StaticBody2D as a child node of the World.

  • Rename it to Ground

  • Add a ColorRect as a child node and resize it to be the width of the playing area.

  • Change the color of the ColorRect by going to the Inspector panel and selecting the required color.

  • Add a CollisionShape2D as a child of the Ground.

  • Select WorldBoundaryShape2D as the collision shape type.

  • group the Ground nodes and move the Ground to the bottom of the playiing area.

STEP 5: Adding a moveable block

  • Add a RigidBody2D child node to the World node.

  • Rename the RigidBody2D to Block.

  • Add a ColorRect node to the Block.

  • Resize the ColorRect as required.

  • Change the color of the ColorRect as required.

  • NOTE: you could add a Sprite2D instead of a ColorRect. I chose a ColorRect for the purpose of this tutorial.

  • Add a CollisionShape2D to the Block.

  • Select a RactangleShape2D as the shape of the CollisionShape2D.

  • Resize the CollisionShape2D to cover the Block.

STEP 6: Let’s save before we go any further!

Press COMMAND + S or CONTROL + S to save your game! Accept the default name which should be world,tscn

STEP 7: Add the Block to a new group

We will now add the Block to a new group so that it can be detected in our script.

  • With the Block selected, go to the Node panel (next to the Inspector panel) and select Groups.

  • Add a new group called Blocks.

STEP 8: Adding the script

Add a new script to the Player node. Accept the default settings as shown and click on the Create button.

STEP 9: Adding constants

Create 2 new constants as follows:

See this content in the original post

STEP 10: Adding the move block code

Just before the moveandslide() method call, add the following code:

See this content in the original post

STEP 11: Save and play scene

Save your game and play your game.

You should notive that you can push over the block.

If you do not want to have the block roll. over then do the following:

  • Select the block node

  • Go to the Inspector panel

  • Open the Deactivation section

  • Click to Lock Rotation

DId you find this tutorial useful?

If you found this tutorial useful then please make a small donation in order to help maintain this website. You will be provided with all resources, including the fully working Godot project, and some bonus materials.