+1 vote

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

in Engine by (47 points)

2 Answers

+2 votes
Best answer

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
by (110 points)
selected by
+1 vote

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.

by (330 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.