Handling Overlapping Objects with CollisionPolygon2D InputEvents

Godot Version

Godot version 4.3 stable

Question

I was using ReferenceRect in order to detect if the mouse was hovering over the card, and if so, allow dragging the card by holding mouse1, everything worked well; when hovering over multiple cards, only the top-most card was increasing in size/selectable. However, the cards are hexagonal-ish, and thus a rectangle isn’t suitable.

After trying a few different nodes; I got the system working with the following:

Card
    CardArt
        ..
    Area2D
        CollisionPolygon2D

image
This allowed the hitbox to follow the hex shape, however, it has a bug, as shown in the image, when hovering multiple cards, all of them increase in size/are selectable, with some print statements, this is the output:

is_card_hovered: true
is_card_hovered: true
is_card_hovered: false
is_card_hovered: false

As I understand CollisionPolygon2D doesn’t work with Z-index ordering for input events.

How do I make it if multiple cards overlap, only the top card is selectable?

Try a combination of Viewport.physics_object_picking_first_only = true and Viewport.physics_object_picking_sort = true.

1 Like