Welcome to Part 2 of this Godot (v4.2.2) tutorial! In this guide, you’ll learn how to create a Doodle Jump clone.
Following up on Part 1, in this tutorial, we will add a platform scene and script to enable the player to jump each time they land on a platform.
1. Setting up the Platform Scene #
For the platform, we create a new scene using an Area2D as the root node. This will enable us to use a handy built-in Godot method that helps launch the player upon detection. Create a new scene named “Platform” and assign the following nodes to it:
- Area2D (Platform Node)
- Sprite2D (Image of the Platform)
- CollisionShape2D (To detect collisions with the player)
2. Platform Script #
To make the player jump upon touching the platform from above, we need to add a script. Please open a new script for the Platform. We chose an Area2D as the main node for the platform because it allows us to use the very handy built-in method on_body_entered, which triggers every time a collision with an object (the player) is detected.
Since we moved the Player to Collision Layer 2, please ensure that the Platform scans for Mask 2 to detect the Player.
To utilize on_body_entered, first select the Platform’s main node and on the right side, next to the Inspector Panel, select the Node panel. Here you will find all the methods that can be used with the Area2D node.
Select on_body_entered and attach it to the Platform Script.
The script should now look something like this:
extends Area2D
func _on_body_entered(body):
if body.velocity.y > 0:
body.velocity.y = -1000
This code does the following:
- Detects when the player collides with the platform.
- Since we only want to launch the player when they are falling, we added an IF statement to check for this condition.
- If the player is falling and hits the platform, we change their velocity to shoot them 1000 units upwards.
One-Way Collision #
Since we want the player to fly through platforms when coming from below, Godot has a handy feature in the CollisionShape2D node that we need to enable: One-Way Collision.
3. Adding the Platform to the Main Scene #
With the platform visual and script completed, we can now add it to the main scene and place it underneath the player. If you play the game at this point, the player should fall as before, but once they hit the platform, they bounce and jump into the air.
Conclusion #
We covered how to create another scene (Platform - Area2D) with logic to make the player jump once they collide with the platform.
Stay tuned for the next tutorial where we will enhance the logic of the platform and add a Spawn Manager to generate platforms randomly as the player jumps higher.