[#gamedev] Pass Through Platforms in Godot Engine
Hey there, in this video we will see how easy is to make pass through platforms in Godot Engine. Here are links mentioned in the video:
Tutorial Repo: http://bit.ly/pass_through_repo
Platform Template: http://bit.ly/platform_template_repo
Ingredients
- A KinematicBody2D
- An Area2D
- 2 StaticBody2D
- 2 CollisionShape2D nodes and their respective Shape2D
- 3 CollisionPolygon2D
- 2 Collision mask bits
- 2 GDScripts or more depending on your implementation
Preparation
Let's start with the setup for the actor, here we add the
KinematicBody2D
, which will be responsible to handle physics, let's also add one CollisionShape2D with its Shape2D being a RectangleShape2DThe second step is to add the
Area2D
and itsCollisionShape2D
, here we will just copy theShape2D
from the KinematicBody2DNow let's block out the playground level by adding a
StatiBody2D
for the floor and another for the pass-through platforms, this one will have 2CollisionShape2D
This step is optional, because not every platform will have this, and you can even say that in your game none will. But I'll add this 2 shapes here to have their
one_way_collision
turned on, this will allow the character to pass through then from bottom to top when jumpingNow let's go to the project settings and prepare 2 collision layer bits, one for general purposes, where the normal floor will be, and one for objects that are meant to be pass-through
And the next thing to setup the environment for this mechanic is to add each object to its respective Collision Layer and set their respective Collision Masks, don't forget that the Area2D acts as a buffer for the collisions, so it searches for collisions in the same layer that the Pass-Through platforms are
KinematicBody2D GDScript
- Create a condition to verify
if event.is_action_pressed(actor.jump)
- Then here we will check
if has_node("pass_through") and Input.is_action_pressed(actor.down)
here goes a tip, when checking for inputs you can either check for an event (something that already happened) and for something that is happening, so in the_input
callback we verify for events, but in this case I want to know if the event happened and at the same time if a given action key is pressed, this is kinda of a way to add actions together - Then here is where the magic happens, we will disable the pass-through mask on the KinematicBody2D, meaning it won't collide with them
actor.set_collision_mask_bit(1, false)
- And if the above condition is not match, we will just tell the Actor to perform a normal
actor.jump()
- Create a condition to verify
Area2D GDScript
- Connect its signal
body_exited
to itself - Inside the method created, we will check
if body.get_collision_layer_bit(1)
, meaning that the body is inside the pass-through collision layer, and then we will also checkand not get_parent().get_collision_mask_bit(1)
meaning the Actor is not checking for collisions in the pass-through layer, i.e. if it performed a pass-through jump - Then here we will
get_parent().set_collision_mask_bit(1, true)
to make the Actor check for collisions in the pass-through layer again.
- Connect its signal
And that's it! If everything goes as expected, your character will be performing a pass-through jump perfectly!
That's it, thank you for watching, keep developing and until the next time!
▶️ DTube
▶️ IPFS
Congratulations @pigdev! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :
You published your First Post
You made your First Vote
You got a First Vote
Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word
STOP
Do not miss the last post from @steemitboard: