Godot provides a sophisticated collision detection system and even a one-way-collision option can be found. The one-way-collision setting only works from top down though. This makes sense for a platformer game, but in a top-down view RPG game, we might need a lot more control.
I created a pretty simple solution that works out really well for me.
Gate Scene
As seen in the picture, there is a vertical group and a horizontal group. Each group contains one StaticBody2D and two Area2Ds and each of these nodes contain a rectangular CollisionShape2D. The body is positioned between the areas and if activated will prevent the moving body from going further. The two areas are used as sensors. They will activate or deactivate the CollisionShape of the StaticBdoy if the moving body is allowed to go trough.
Right now the gate has multiple modes which are controlled by an enum. I also included the option open_once which lets the body through once. After that the gate is completely blocked.
If needed, I will include the option to check for groups in the future. This would allow me to only let npcs or players pass through.
enum GateType { OPEN, V_CLOSED, H_CLOSED, TOP_OPEN, BOTTOM_OPEN, LEFT_OPEN, RIGHT_OPEN }
@export var gate_type: GateType = GateType.OPEN
@export var open_once: bool = false
Collision Settings
The StaticBody2D nodes are on the collision layer world, which is generally used as a blocking layer. The collision mask is set to the player and the npc layer which do what their names suggest.
Comments
👁️