Hi everyone! I’m encountering an issue in my Godot (4.2.2) project related to collision layers and input handling.
I’m trying to change the collision layer and mask of my player character when the player presses and releases the ui_down
action while standing on a platform. The logic works correctly sometimes but fails intermittently, especially after trying a few times. Sometimes it’s possible to get back on the platform 3 times, sometimes 2, sometimes even just once.
The platform consists of a Sprite2D
and a CollisionShape2D
with an extra Area2D
(which parents a CollisionShape2D
) on top to detect if the player is standing by the means of a body_entered
signal.
Here is the code for the platform:
extends AnimatableBody2D
var standing_on_platform = false
func _on_area_2d_body_entered(body):
standing_on_platform = true
func _on_area_2d_body_exited(body):
standing_on_platform = false
func _input(event):
if standing_on_platform:
if event.is_action_pressed("ui_down"):
set_collision_layer_value(2, false)
elif event.is_action_released("ui_down"):
set_collision_layer_value(2, true)
And I’m also attaching a picture of the platform itself.
I tried changing from _input(event)
to something like _process
and even _physics_process
but nothing really helped. The collision layers and masks are working so I’m thinking this is a more deeper problem, like on how Godot draws each frame or something.
A small update: I managed to replicate the bug. Apparently, tapping ui_down
very fast won’t cause the bug. The problem occurs if the key is hold for a while (even a very short period of time will make it stop working). I tried changing the code to .is_action_just_pressed
with no success.
Anyway, any idea of what could be happening here?