How to detect if mouse is hovering over a MeshInstance2D object in C#?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By markotaht

I have a C# script attached to MeshInstance2d object. Script generates an Hexagonal Arraymesh with uv-s. How can i detect if a mouse is hovering one of these hexagons? As everything is generated within code, i cant use editor methods to generate collision shapes and what not, so everything that needs to be generated, must be generated within code.

I understand that i need a collision shape, but how do i make it and where to i but it?

:bust_in_silhouette: Reply From: Zylann

Assuming your node won’t move, add a Area2D to it with input_pickable set to true (so it will receive input events), and then a CollisionPolygon2D as child. You can then generate the points of the polygon into a hexagon : CollisionPolygon2D — Godot Engine (stable) documentation in English
It works the same in C# or GDScript.

You may have to learn how collisions work in Godot 2D: Physics — Godot Engine (stable) documentation in English