How to get node by position?

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

How to detect, what nodes intersect some coordinates (for example mouse coordinates)?

:bust_in_silhouette: Reply From: Toshio Araki

See this tutorial:

In particular this part (for 3D just use get_world and Vector3 as parameters):

var space_state = get_world_2d().direct_space_state
    # use global coordinates, not local to node
    var result = space_state.intersect_ray(Vector2(0, 0), Vector2(50, 100))

You will get a dictionary as a response, and, I’m guessing, the collider is the node you intersect with.

{
   position: Vector2 # point in world space for collision
   normal: Vector2 # normal in world space for collision
   collider: Object # Object collided or null (if unassociated)
   collider_id: ObjectID # Object it collided against
   rid: RID # RID it collided against
   shape: int # shape index of collider
   metadata: Variant() # metadata of collider
}
:bust_in_silhouette: Reply From: MagnusS

Alternatively you could add a collision object to your node and use its _input_event() to react to a mouse click if that’s what you want to do.