[#gamedev] Pass Through Platforms in Godot Engine

in #gamedev6 years ago (edited)


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

  1. 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 RectangleShape2D

  2. The second step is to add the Area2D and its CollisionShape2D, here we will just copy the Shape2D from the KinematicBody2D

  3. Now 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 2 CollisionShape2D

  4. 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 jumping

  5. Now 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

  6. 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

  7. KinematicBody2D GDScript

    1. Create a condition to verify if event.is_action_pressed(actor.jump)
    2. 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
    3. 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)
    4. And if the above condition is not match, we will just tell the Actor to perform a normal actor.jump()
  8. Area2D GDScript

    1. Connect its signal body_exited to itself
    2. 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 check and 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
    3. 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.

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
Sort:  

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:

SteemitBoard - Witness Update

Support SteemitBoard's project! Vote for its witness and get one more award!

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.033
BTC 62934.09
ETH 3118.65
USDT 1.00
SBD 3.85